isama92

Create a JSON with every image's path in a dir and subdirs

Apr 11th, 2017
803
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.86 KB | None | 0 0
  1. <?php
  2.  
  3.   // DIRECTORY WHERE YOU'LL SEARCH FOR IMAGES
  4.   //$directory = ".";
  5.   $directory = "img/" . $_REQUEST["dir"];
  6.  
  7.  
  8.  
  9.   // INITIALIZE ARRAY
  10.   $img_array = Array();
  11.   // LAUNCH FUNCTION
  12.   $img_array = listFolderFiles($directory, $img_array);
  13.   // PRINT JSON
  14.   echo json_encode($img_array);
  15.  
  16.  
  17.  
  18. // FUNZIONE
  19. function listFolderFiles($dir, $img_array){
  20.   $files = scandir($dir);
  21.   foreach($files as $file)
  22.     if($file != '.' && $file != '..')
  23.       if(is_dir($dir.'/'.$file))
  24.         $img_array = listFolderFiles($dir.'/'.$file, $img_array);
  25.       else {
  26.         $file_exploded = explode(".", $file);
  27.         $file_extension = strtolower($file_exploded[count($file_exploded)-1]);
  28.         if($file_extension == "jpg" || $file_extension == "png"){
  29.           array_push($img_array, $dir . "/" . $file);
  30.         }
  31.       }
  32.   return $img_array;
  33. }
  34.  
  35. ?>
Advertisement
Add Comment
Please, Sign In to add comment