Wuhtzu Posted May 13, 2007 Share Posted May 13, 2007 Hey I have a directory /folder (which is actually /usr/home/web/[customer id]/folder when it comes to working with opendir()) and I would like to populate an array with name and location of all files found in the directory and it's subdirectories. Below are an example of a directory structure and the array I want to create from it: Directory structure: +/folder |--file1.ext |--file2.ext | |-+/subdir1 | |--file1.ext | |--file2.ext | |--+/subdir2 |--file3.ext |--file4.ext |-+/subsubdir1 | |--file3.ext | |--file4.ext Desired array: Array ( [0] => Array ( [file] => file1.ext [link] => /folder/file1.ext ) [1] => Array ( [file] => file2.ext [link] => /folder/file2.ext ) [2] => Array ( [file] => file1.ext [link] => /folder/subdir1/file1.ext ) [3] => Array ( [file] => file2.ext [link] => /folder/subdir1/file2.ext ) [4] => Array ( [file] => file3.ext [link] => /folder/subdir2/file3.ext ) [5] => Array ( [file] => file4.ext [link] => /folder/subdir2/file4.ext ) [6] => Array ( [file] => file3.ext [link] => /folder/subdir2/subsubdir1/file3.ext ) [6] => Array ( [file] => file4.ext [link] => /folder/subdir2/subsubdir1/file4.ext ) This is what I have come up with so far: get_files_in_dir.php: <?PHP function get_files_in_dir($dir) { //Check if opendir() can use $dir and open it if($handle = opendir($dir)) { //Loop through $dir $i = 0; while (false !== ($file = readdir($handle))) { //Don't bother with the . and .. file/dir if($file != "." && $file != "..") { //The full path to the file (or directory if $file is a dir) $link = $dir . "/" . $file; //Check if $file is a dir if(is_dir($link)){ //The file was a dir so loop through that one too get_files_in_dir($link); } else{ //The file was not a dir so it gets stored in the //array $files[]. Both it's filename and path $files[$i]["file"] = $file; $files[$i]["link"] = $link; $i++; } } } //Close the dir closedir($handle); //Print the array, just for the test phase print_r($files); } else { return false; } } //call the function: $dir = "/usr/home/web/web115029/random/folder"; get_files_in_dir($dir); ?> The function is "live" here and you can see its output - remember to view the source of the page for readable layout: http://wuhtzu.dk/random/get_files_in_dir.php As you can see my function creates a new array for each directory it finds because each time it finds a directory it calls it self again. Have you go any suggestions on how to proceed from here to achieve my goal? Best regards Wuhtzu Quote Link to comment https://forums.phpfreaks.com/topic/51186-solved-list-all-files-in-a-dir-and-subdirs/ Share on other sites More sharing options...
Wuhtzu Posted May 13, 2007 Author Share Posted May 13, 2007 No suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/51186-solved-list-all-files-in-a-dir-and-subdirs/#findComment-252173 Share on other sites More sharing options...
Wuhtzu Posted May 13, 2007 Author Share Posted May 13, 2007 I got it right: <?PHP function get_files_in_dir($dir) { // Verify that $dir is a directory if(is_dir($dir)) { // $dirs_to_scan[] contains all the direcotries to scan. It begins by // containing only $dir (as specified in the function call, but gets // populated as more directories are found inside the orginal $dir $dirs_to_scan[] = $dir; // count directories $i = 0; // count files $j = 0; // loop through $dirs_to_scan. Runs as long as $dirs_to_scan holds more entries than // have already been scanned and counted by $i while($i < count($dirs_to_scan)) { //Create handle for the current dir to scan $handle = opendir($dirs_to_scan[$i]); //Loop through the current dir ($dirs_to_scan[$i] while (false !== ($file = readdir($handle))) { //Don't bother with the . and .. file/dir if($file != "." && $file != "..") { //The full path to the file (or directory if $file is a dir) $link = $dirs_to_scan[$i] . "/" . $file; //Check if $file is a dir if(is_dir($link)){ //$file was a dir so sign it up for scan $dirs_to_scan[] = $link; } else{ //The file was not a dir so it gets stored in the //array $files[]. Both it's filename and path $files[$j]["filename"] = $file; $files[$j]["file"] = $link; //Add to the file count $j++; } } } //Close the currently open dir closedir($handle); //Add to the directory count $i++; } // Print the $dirs_to_scan array - JUST FOR THE TEST PHASE print_r($dirs_to_scan); // New lines to improve readability - JUST FOR THE TEST PHASE echo "\n\n\n"; // Print the $files array - JUST FOR THE TEST PHASE print_r($files); } else { return false; } } //call the function: $dir = "/usr/home/web/web115029/random"; get_files_in_dir($dir); ?> The function is "live" here: http://wuhtzu.dk/random/get_files_in_dir_working.php (remember to view page source to get it readable) Thanks to those who checked by... Wuhtzu Quote Link to comment https://forums.phpfreaks.com/topic/51186-solved-list-all-files-in-a-dir-and-subdirs/#findComment-252314 Share on other sites More sharing options...
chigley Posted May 13, 2007 Share Posted May 13, 2007 Why not echo <pre> before your code and </pre> after it to save you having to view source all the time? Quote Link to comment https://forums.phpfreaks.com/topic/51186-solved-list-all-files-in-a-dir-and-subdirs/#findComment-252318 Share on other sites More sharing options...
Wuhtzu Posted May 13, 2007 Author Share Posted May 13, 2007 Do you mean echo for each cycle in the loop? Well if I just wanted to see the output there wouldn't be any point in storing it in an array. It's for a file search function, so the print_r() is just for the testing the script and print_r() just looks horrible if you do not look at the source Quote Link to comment https://forums.phpfreaks.com/topic/51186-solved-list-all-files-in-a-dir-and-subdirs/#findComment-252432 Share on other sites More sharing options...
chigley Posted May 14, 2007 Share Posted May 14, 2007 Sorry, the forum parsed what I said... this Is what I meant to say: <pre> before your code and </pre> after it Quote Link to comment https://forums.phpfreaks.com/topic/51186-solved-list-all-files-in-a-dir-and-subdirs/#findComment-252628 Share on other sites More sharing options...
Wuhtzu Posted May 14, 2007 Author Share Posted May 14, 2007 wow, neat trick. I guess everyone except me knew that was the way to make print_r readable I'll remember that. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/51186-solved-list-all-files-in-a-dir-and-subdirs/#findComment-252659 Share on other sites More sharing options...
neel_basu Posted May 14, 2007 Share Posted May 14, 2007 The Proper Way to do this is header('Content-Type: text/plain'); At the Very Top of your Page. Quote Link to comment https://forums.phpfreaks.com/topic/51186-solved-list-all-files-in-a-dir-and-subdirs/#findComment-252665 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.