How To Display A Random Image From Directory In PHP?

By Faruque Ahamed Mollick

Suppose we have a directory that is contained with some images and we want to display one of those images on our web page. The image that will be displayed on the web page will randomly select from the directory. We are going to do this in PHP.

Easy to use PHP function to show random image from a particular directory

Let’s follow the steps.

Ate first, get the image directory name in a variable and initiate an array that will contain all the image name later. After that, check if the directory exists or not using PHP file_exists and is_dir function:

$dir = 'my_directory';
// Initiate array which will contain the image name
$imgs_arr = array();

// Check if image directory exists
if (file_exists($dir) && is_dir($dir) ) {
    // Run code if the directory exists
 }

The code that is going to be run if the directory exists is given below:

// Get files from the directory
$dir_arr = scandir($dir);
$arr_files = array_diff($dir_arr, array('.','..') );

foreach ($arr_files as $file) {
  //Get the file path
  $file_path = $dir."/".$file;
  // Get extension
  $ext = pathinfo($file_path, PATHINFO_EXTENSION);

  if ($ext=="jpg" || $ext=="png" || $ext=="JPG" || $ext=="PNG") {
    array_push($imgs_arr, $file);
  }
  
}

$count_img_index = count($imgs_arr) - 1;
$random_img = $imgs_arr[rand( 0, $count_img_index )];

In the above code, we have used the PHP scandir() function to fetch all the files in the directory. Then we remove the parent and child directory indicator using PHP array_diff function.

Then we run a foreach loop to get each file name from our directory. Here we are getting all the files that also may not be an image. So we have checked the extension. In our condition, we have taking only PNG and JPG type image files and pushing it to our array that we initiated using array_push function.

Easy to use PHP function to show random image from a particular directory

Listing files from directory that have particular extensions in PHP

All the image files are now storing in our array. We have subtracted the total number of the array index in the array by 1 so that the maximum index number matches the number. After that, we taking the random array index between 0 and the maximum number to get the image file name.

Now, below is the code that will show a random image from the directory on our web page:

<img src="<?php echo $dir."/".$random_img ?>">

Complete code to display a random image from directory

Below is the complete code that will display the image on our web page that is randomly selected from a particular directory:

<?php

  $dir = 'my_directory';
  // Initiate array which will contain the image name
  $imgs_arr = array();

  // Check if image directory exists
  if (file_exists($dir) && is_dir($dir) ) {
    
      // Get files from the directory
      $dir_arr = scandir($dir);
      $arr_files = array_diff($dir_arr, array('.','..') );

      foreach ($arr_files as $file) {
        //Get the file path
        $file_path = $dir."/".$file;
        // Get extension
        $ext = pathinfo($file_path, PATHINFO_EXTENSION);

        if ($ext=="jpg" || $ext=="png" || $ext=="JPG" || $ext=="PNG") {
          array_push($imgs_arr, $file);
        }
        
      }

      $count_img_index = count($imgs_arr) - 1;
      $random_img = $imgs_arr[rand( 0, $count_img_index )];

  }

?>

<img src="<?php echo $dir."/".$random_img ?>">

 

Leave a Reply

Your email address will not be published. Required fields are marked *