PHP 7 Script to Display Images and Files of Particular Extensions From Directory in Browser Full Project For Beginners

  • Post author:
  • Post category:PHP
  • Post comments:0 Comments


Welcome folks today in this blog post we will be displaying images and files of particular extensions from directory in browser using php 7.All the full source code of the application is given below.




Get Started




In order to get started you need to make an index.php file and copy paste the following code








  $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";


Leave a Reply