PHP 7 Script to Display Images and Files of Particular Extensions From Directory in Browser

This article is a very important article. I mean in this PHP 7 Script to Display Images and Files of Particular Extensions From Directory in Browser we are going to know very clearly about this. We hope you like this article. Please comment if you liked this article.

PHP 7 Script to Display Images and Files of Particular Extensions From Directory in Browser

PHP 7 Script to Display Images and Files of Particular Extensions From Directory in Browser

<?php

  $dir = 'my_directory';

  // Check if the directory exists
  if (file_exists($dir) && is_dir($dir) ) {
    
      // Get the files of the directory as an array
      $scan_arr = scandir($dir);
      $files_arr = array_diff($scan_arr, array('.','..') );

      // echo "<pre>"; print_r( $files_arr ); echo "</pre>";

      // Get each files of our directory with line break
      foreach ($files_arr as $file) {
        //Get the file path
        $file_path = "my_directory/".$file;
        // Get the file extension
        $file_ext = pathinfo($file_path, PATHINFO_EXTENSION);
        if ($file_ext=="jpg" || $file_ext=="png" || $file_ext=="JPG" || $file_ext=="PNG") {
          echo $file."<br/>";
        }
        
      }
  }
  else {
    echo "Dorectory does not exists";
  }

?>

Read Also: Kotlin Program to Find Factorial of a Number Using Recursion

Final Words

PHP 7 Script to Display Images and Files of Particular Extensions From Directory in Browser We have clearly identified the information you need through the article. If in doubt please let us know your doubts via the comment box Thank you.

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Deekshi. Here, I post about programming to help developers.

Share on:

Leave a Comment