tomfmason Posted September 4, 2006 Share Posted September 4, 2006 I am attempting to look through a directory and record all sub directories/files into two arrays. Here is what I have so far.[code]<?phpclearstatcache();$folders = array();$files = array();$path = "./";if ($dir = opendir($path)) { $d = 0; $f = 0; while (false !== ($file = readdir($dir))) { if ($file != '.' && $file != '..') [ if (is_dir($file) !== false) { $folders[$d] = $file; $d++; //I want to loop through these folders and add any sub folders as well as the files //to the files array } if (is_file($file) !== false) { $files[$f] = "main, $file"; $f++; } } }}closedir($dir);?>[/code]I tried doing another while loop in the [code=php:0]if (is_dir($file) !== false) {[/code] but that timed out. Any suggestions as to a better way to do this would be great.Thanks,Tom Quote Link to comment https://forums.phpfreaks.com/topic/19635-suggestions-needed/ Share on other sites More sharing options...
marker5a Posted September 4, 2006 Share Posted September 4, 2006 Looks about as simple as it can get... cant you just change the timeout time in the ini file? Quote Link to comment https://forums.phpfreaks.com/topic/19635-suggestions-needed/#findComment-85523 Share on other sites More sharing options...
tomfmason Posted September 4, 2006 Author Share Posted September 4, 2006 I guess I could but I already have the time out set to 60 seconds. I don't realy want the script to take more time then that.Any other suggestions?Thanks again,Tom Quote Link to comment https://forums.phpfreaks.com/topic/19635-suggestions-needed/#findComment-85535 Share on other sites More sharing options...
Jenk Posted September 4, 2006 Share Posted September 4, 2006 recursion is what you need, and a lot of patience.[code]<?phpfunction filelist($dir){ if (!$dir = realpath($dir)) return null; static $files = array(); static $dirs = array(); $handle = opendir($dir); while (($file = readdir($handle)) !== false) { if (!in_array($file, array('.', '..'))) { if (is_dir($path = ($dir . DIRECTORY_SEPARATOR . $file))) { $dirs[] = $path; filelist($path); } else { $files[] = $path; } } } return array('dirs' => $dirs, 'files' => $files);}echo nl2br(print_r(filelist('.'), true));?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/19635-suggestions-needed/#findComment-85639 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.