Getting the number of files in a folder


I have a random image code from a website which looks like this below



  • Name your images 1.jpg, 2.jpg etc.
  • Add this line to your page where you want the images to
  • appear: <?php include "randomimage.php"; ?>

// Change this to the total number of images in the folder
$total = “11”;
$file_type = “.jpg”;
// Change to the location of the folder containing the images
$image_folder = “images/random”;
// You do not need to edit below this line
$start = “1”;
$random = mt_rand($start, $total);
$image_name = $random . $file_type;
echo “$image_name”;

I want to be able to add the files to the folder without having to change the total in this file every time and was wondering if there is a way to do this? Or possibly a code to detect how many files are in the folder? And if not if any one has any ideas how I can get around it.

I am stuck on this one :o


Try looking into opendir(), readdir(), and closedir().


Thanks that’s great! :D