Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $rootDir = '/path/to/folder';
- function scanDirectories($rootDir) {
- // set filenames invisible if you want
- $invisibleFileNames = array(".", "..", ".htaccess", ".htpasswd");
- // run through content of root directory
- $dirContent = scandir($rootDir);
- $allData = array();
- // file counter gets incremented for a better
- $fileCounter = 0;
- foreach($dirContent as $key => $content) {
- // filter all files not accessible
- $path = $rootDir.'/'.$content;
- if(!in_array($content, $invisibleFileNames)) {
- // if content is file & readable, add to array
- if(is_file($path) && is_readable($path)) {
- $tmpPathArray = explode("/",$path);
- // saving filename
- $allData[$fileCounter]['fileName'] = end($tmpPathArray);
- // saving while path (for better access)
- $allData[$fileCounter]['filePath'] = $path;
- // get file extension
- $filePartsTmp = explode(".", end($tmpPathArray));
- $allData[$fileCounter]['fileExt'] = end($filePartsTmp);
- // get file date
- $allData[$fileCounter]['fileDate'] = filectime($path);
- // get filesize in byte
- $allData[$fileCounter]['fileSize'] = filesize($path);
- $fileCounter++;
- // if content is a directory and readable, add path and name
- }elseif(is_dir($path) && is_readable($path)) {
- $dirNameArray = explode('/',$path);
- $allData[$path]['dirPath'] = $path;
- $allData[$path]['dirName'] = end($dirNameArray);
- // recursive callback to open new directory
- $allData[$path]['content'] = scanDirectories($path);
- }
- }
- }
- return $allData;
- }
- echo json_encode(scanDirectories($rootDir));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement