Advertisement
Guest User

php recursive

a guest
Apr 28th, 2012
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.88 KB | None | 0 0
  1. $rootDir = '/path/to/folder';
  2.  
  3. function scanDirectories($rootDir) {
  4.     // set filenames invisible if you want
  5.     $invisibleFileNames = array(".", "..", ".htaccess", ".htpasswd");
  6.     // run through content of root directory
  7.     $dirContent = scandir($rootDir);
  8.     $allData = array();
  9.     // file counter gets incremented for a better
  10.     $fileCounter = 0;
  11.     foreach($dirContent as $key => $content) {
  12.         // filter all files not accessible
  13.         $path = $rootDir.'/'.$content;
  14.         if(!in_array($content, $invisibleFileNames)) {
  15.             // if content is file & readable, add to array
  16.             if(is_file($path) && is_readable($path)) {
  17.                 $tmpPathArray = explode("/",$path);
  18.                 // saving filename
  19.                 $allData[$fileCounter]['fileName'] = end($tmpPathArray);
  20.                 // saving while path (for better access)
  21.                 $allData[$fileCounter]['filePath'] = $path;
  22.                 // get file extension
  23.                 $filePartsTmp = explode(".", end($tmpPathArray));
  24.                 $allData[$fileCounter]['fileExt'] = end($filePartsTmp);
  25.                 // get file date
  26.                 $allData[$fileCounter]['fileDate'] = filectime($path);
  27.                 // get filesize in byte
  28.                 $allData[$fileCounter]['fileSize'] = filesize($path);
  29.                 $fileCounter++;
  30.             // if content is a directory and readable, add path and name
  31.             }elseif(is_dir($path) && is_readable($path)) {
  32.                 $dirNameArray = explode('/',$path);
  33.                 $allData[$path]['dirPath'] = $path;
  34.                 $allData[$path]['dirName'] = end($dirNameArray);
  35.                 // recursive callback to open new directory
  36.                 $allData[$path]['content'] = scanDirectories($path);
  37.             }
  38.         }
  39.     }
  40.     return $allData;
  41. }
  42.  
  43. echo json_encode(scanDirectories($rootDir));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement