Advertisement
Guest User

Untitled

a guest
Jun 30th, 2016
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. <?php
  2.  
  3. $dir = "files";
  4.  
  5. // Run the recursive function
  6.  
  7. $response = scan($dir);
  8.  
  9.  
  10. // This function scans the files folder recursively, and builds a large array
  11.  
  12. function scan($dir){
  13.  
  14. $files = array();
  15.  
  16. // Is there actually such a folder/file?
  17.  
  18. if(file_exists($dir)){
  19.  
  20. foreach(scandir($dir) as $f) {
  21.  
  22. if(!$f || $f[0] == '.') {
  23. continue; // Ignore hidden files
  24. }
  25.  
  26. if(is_dir($dir . '/' . $f)) {
  27.  
  28. // The path is a folder
  29.  
  30. $files[] = array(
  31. "name" => $f,
  32. "type" => "folder",
  33. "path" => $dir . '/' . $f,
  34. "items" => scan($dir . '/' . $f) // Recursively get the contents of the folder
  35. );
  36. }
  37.  
  38. else {
  39.  
  40. // It is a file
  41.  
  42. $files[] = array(
  43. "name" => $f,
  44. "type" => "file",
  45. "path" => $dir . '/' . $f,
  46. "size" => filesize($dir . '/' . $f) // Gets the size of this file
  47. );
  48. }
  49. }
  50.  
  51. }
  52.  
  53. return $files;
  54. }
  55.  
  56.  
  57.  
  58. // Output the directory listing as JSON
  59.  
  60. header('Content-type: application/json');
  61.  
  62. echo json_encode(array(
  63. "name" => "files",
  64. "type" => "folder",
  65. "path" => $dir,
  66. "items" => $response
  67. ));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement