birman Posted July 9, 2007 Share Posted July 9, 2007 Hi How can I do count all files within all directories contained in one directory? Example: /root /file1.txt -dir1/file1.jpg -dir1/dir_2/file5.png, others.doc -dir1/dir_2/otherfile.php ........ Total: 5 files Thanks Link to comment https://forums.phpfreaks.com/topic/59074-simple-count-files/ Share on other sites More sharing options...
Yesideez Posted July 9, 2007 Share Posted July 9, 2007 You mean you want to show the files on the server? http://www.php.net/scandir That might be what you're looking for. Link to comment https://forums.phpfreaks.com/topic/59074-simple-count-files/#findComment-293264 Share on other sites More sharing options...
birman Posted July 9, 2007 Author Share Posted July 9, 2007 yes, i want only count the files in a path. I've used this code but it doesn't go beyond the ./dir1 $files = scandir("./dir1", 1); echo "<pre>"; var_dump($files); echo "</pre>"; Link to comment https://forums.phpfreaks.com/topic/59074-simple-count-files/#findComment-293297 Share on other sites More sharing options...
Yesideez Posted July 9, 2007 Share Posted July 9, 2007 First, no need to have "./" in your path as you're pointing to the same folder. Use "../" if you want to go back a folder. Link to comment https://forums.phpfreaks.com/topic/59074-simple-count-files/#findComment-293299 Share on other sites More sharing options...
birman Posted July 9, 2007 Author Share Posted July 9, 2007 i use ./dir1 because i have the script and the dir1 in the same folder. and i count all the file in all the directories contained in the ./dir1 Link to comment https://forums.phpfreaks.com/topic/59074-simple-count-files/#findComment-293336 Share on other sites More sharing options...
Yesideez Posted July 9, 2007 Share Posted July 9, 2007 scandir will onyl scan the current folder. If you want it to look inside folders it finds you'll have to write a routine to recursively run through the folders it finds. Link to comment https://forums.phpfreaks.com/topic/59074-simple-count-files/#findComment-293339 Share on other sites More sharing options...
birman Posted July 9, 2007 Author Share Posted July 9, 2007 can you post a example code of a routine to read more directories, please? Link to comment https://forums.phpfreaks.com/topic/59074-simple-count-files/#findComment-293365 Share on other sites More sharing options...
Yesideez Posted July 9, 2007 Share Posted July 9, 2007 I've not written one myself as I've not had the need to but I'm sure you can find one if you head towards google? This may be of help: http://lixlpixel.org/recursive_function/php/get_size_recursively/ Link to comment https://forums.phpfreaks.com/topic/59074-simple-count-files/#findComment-293369 Share on other sites More sharing options...
GingerRobot Posted July 9, 2007 Share Posted July 9, 2007 Got bored, so i put this together: <?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; $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'); ?> Tested and compared with the number of files reported by windows. Seems to work fine to me. Link to comment https://forums.phpfreaks.com/topic/59074-simple-count-files/#findComment-293409 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.