Bricktop Posted September 7, 2009 Share Posted September 7, 2009 Hi all, I have the following code which reads the files in a given directory and outputs the filenames to an array. It works great but I wonder if anyone can tell me how to amend it so it can also look through subdirectories. Basically I would like subdirectories to be held in an array, and the the files contained within those subdirectories to be held in an array inside the relevant subdomain array (if that makes sense?) So, basically I could do: foreach($subdirectory) { echo $subdirectoryname; foreach($filename) { echo $filename; } } Anyway, here's my current code: // create an array to hold directory list $results = array(); // create a handler for the directory $handler = opendir('backups/'); // keep going until all files in directory have been read while ($file = readdir($handler)) { // if $file isn't this directory or its parent, // add it to the results array if ($file != '.' && $file != '..') $results[] = $file; } // tidy up: close the handler closedir($handler); Any ideas welcome! Thanks Link to comment https://forums.phpfreaks.com/topic/173393-solved-recursive-folder-reading/ Share on other sites More sharing options...
bundyxc Posted September 7, 2009 Share Posted September 7, 2009 Maybe make an array of files and folders with scandir and then run through your array with the isdir function on each? If isdir() returns true, you can opendir() that directory, and when isdir() returns false on the entire scandir() array, you can go up one folder and start where you left off. I'm not sure. That almost sounds too complicated... Link to comment https://forums.phpfreaks.com/topic/173393-solved-recursive-folder-reading/#findComment-914036 Share on other sites More sharing options...
Mark Baker Posted September 7, 2009 Share Posted September 7, 2009 There's plenty of examples of recursively traversing a file tree in the PHP documentation on www.php.net using either readdir(), scandir(), glob or even SPL Link to comment https://forums.phpfreaks.com/topic/173393-solved-recursive-folder-reading/#findComment-914047 Share on other sites More sharing options...
Bricktop Posted September 7, 2009 Author Share Posted September 7, 2009 Thanks chaps! Link to comment https://forums.phpfreaks.com/topic/173393-solved-recursive-folder-reading/#findComment-914055 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.