MasterACE14 Posted January 7, 2008 Share Posted January 7, 2008 Hello Everyone, Is there a way I can loop through all the txt documents in a folder, and count how many there is? Regards ACE Quote Link to comment https://forums.phpfreaks.com/topic/84801-solved-grabbing-amount-of-txt-files-in-a-directory/ Share on other sites More sharing options...
Wuhtzu Posted January 7, 2008 Share Posted January 7, 2008 Simply loop over the directory and check if the file meet your requirements and if it does add 1 to a counter: <?php $num_of_text_files = 0; if ($handle = opendir('/path/to/files')) while (false !== ($file = readdir($handle))) { if(preg_match("/\.txt/$", $file)) { $num_of_txt_files++; } } closedir($handle); } echo "There is $num_of_text_files files with the extension .txt in the folder"; ?> Of course you can do your "testing" different.... preg_match just seemed easiest for me to write Edit: Corrected a weird sentence and I missed a ) in an if-statement Quote Link to comment https://forums.phpfreaks.com/topic/84801-solved-grabbing-amount-of-txt-files-in-a-directory/#findComment-432315 Share on other sites More sharing options...
MasterACE14 Posted January 7, 2008 Author Share Posted January 7, 2008 ok, thanks Quote Link to comment https://forums.phpfreaks.com/topic/84801-solved-grabbing-amount-of-txt-files-in-a-directory/#findComment-432326 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.