AbydosGater Posted July 11, 2007 Share Posted July 11, 2007 Hey guys. Im working on a directory and file lister.. that will obviously display all files and directorys within a given path. The code i am using is as follows: <?php set_time_limit(0); function scan_dir($DIR){ global $dirs; $dp = opendir($DIR); $dirs = array(); while (false !== ($file = readdir($dp))){ if (is_dir($DIR .$file)){ if($file !== '.' && $file !== '..'){ $dirs[] = $DIR.$file.'/'; echo $DIR.$file.'/'.'<br />'; } } } } scan_dir("path/to/scan/"); while ($dirs){ foreach ($dirs as $dir){ scan_dir($dir); } } ?> Now. I tried it within the folder its in (dir_reader) and within there i created some other folders and sub folders.. The output was as follows: c:/wamp/www/dir_reader/IM A DIR/ c:/wamp/www/dir_reader/IM A DIR/ME2/ c:/wamp/www/dir_reader/IM A DIR/SO AM I/ c:/wamp/www/dir_reader/IM A DIR/SO AM I/ME3/ c:/wamp/www/dir_reader/IM A DIR/SO AM I/ME3/ME4/ c:/wamp/www/dir_reader/IM A DIR/SO AM I/ME3/ME4/ME5/ c:/wamp/www/dir_reader/IM A DIR/SO AM I/ME3/ME4/ME5/ME6/ So as you can see i would expect it to be working completely. But if i run it on my full /www/ dir which has maybe 30 folders some with maybe 30 subfolders and it goes on forever lol .. so i want to list everything. But when i do. It doesnt list everything... It lists quite a few folders really.. But nowhere near as many as there is to be listed. Can anyone see what it stops listing directorys? Its nothing to do with time limits cause my max_execution_time is 30 secs but, this only take less then 1 to load. Anyone see anything wrong? Andy Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted July 12, 2007 Share Posted July 12, 2007 You need to use recursion... Here is an example of what you are wanting to do: http://us.php.net/manual/en/function.readdir.php#75156 Notice how when the script is going through the files in the directory it's looking through, if it sees that the file is actually a directory it calls itself to parse that directory. Quote Link to comment Share on other sites More sharing options...
AbydosGater Posted July 13, 2007 Author Share Posted July 13, 2007 Ahh, This works! Thanks. I understand you must parse each directory as you come to it. Not save it and do it later like i was. Thanks Andy Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted July 13, 2007 Share Posted July 13, 2007 You might find this useful. I made this for another topic somwhere: <?php function no_of_files($start_dir,$sub_dir=FALSE){ $no_of_files=0; if($sub_dir === false){ $handler = opendir($start_dir); }else{ $start_dir = $start_dir.$sub_dir; $handler = opendir($start_dir); } while(false !== ($file = readdir($handler))){ if($file != '.' && $file != '..'){ if(is_dir($start_dir.'\\'.$file)){//if this is a directory, recall the function with the subdirectory defined $dir_name = '\\'.$file; echo $dir_name.'<br />'; $no_of_files += no_of_files($start_dir,$dir_name); }else{//is a file, so increase our counter $no_of_files++; } } } return $no_of_files; } echo no_of_files('C:\Documents and Settings\Ben\My Documents\My Music'); ?> This was to return the number of files(not folders) in a given directory and all its sub directories. Could be modified to echo names and include folders etc Quote Link to comment Share on other sites More sharing options...
AbydosGater Posted July 13, 2007 Author Share Posted July 13, 2007 Ohhhhh I didnt even think of anything like that! Thank you so much. This will come in very useful! Andy Quote Link to comment 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.