Shprqness

Untitled

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