Kitty Posted February 15, 2007 Share Posted February 15, 2007 Hello everyone who's reading this! I have a script that counts how many files I have in a specific folder, but I recently added index.html files with a redirect script in them to all of my folders and now I don't want my script to count that index.html file. So, is it possible to make it count all files except for 1? This is the script I'm using: <?php $path_layouts = opendir("/home/***/public_html/graphics/layouts"); while ($directory_layouts = readdir($path_layouts)) { if ($directory_layouts != "." && $directory_layouts != "..") { $counter_layouts++; } } echo $counter_layouts; closedir($path_layouts); ?> Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/38658-counting-files-in-a-folder/ Share on other sites More sharing options...
papaface Posted February 15, 2007 Share Posted February 15, 2007 If its just one file you could just do a simple -1 calculation. But im sure there is a more appropriate method. Quote Link to comment https://forums.phpfreaks.com/topic/38658-counting-files-in-a-folder/#findComment-185596 Share on other sites More sharing options...
utexas_pjm Posted February 15, 2007 Share Posted February 15, 2007 <?php $path_layouts = opendir("/home/***/public_html/graphics/layouts"); while ($directory_layouts = readdir($path_layouts)) { if ($directory_layouts != "." && $directory_layouts != ".." && $directory_layouts != "index.html") { $counter_layouts++; } } echo $counter_layouts; closedir($path_layouts); ?> Quote Link to comment https://forums.phpfreaks.com/topic/38658-counting-files-in-a-folder/#findComment-185597 Share on other sites More sharing options...
JJohnsenDK Posted February 15, 2007 Share Posted February 15, 2007 Why not just do this: <?php $path_layouts = opendir("/home/***/public_html/graphics/layouts"); while ($directory_layouts = readdir($path_layouts)) { if ($directory_layouts != "." && $directory_layouts != ".." || $directory_layouts != "index.html") { $counter_layouts++; } } echo $counter_layouts; closedir($path_layouts); ?> If you have several files that you dont want counted put them in an array... Quote Link to comment https://forums.phpfreaks.com/topic/38658-counting-files-in-a-folder/#findComment-185598 Share on other sites More sharing options...
JJohnsenDK Posted February 15, 2007 Share Posted February 15, 2007 Woops, sorry... kinda stol the solution from utexas_pjm. Sorry mate, didnt see your reply Quote Link to comment https://forums.phpfreaks.com/topic/38658-counting-files-in-a-folder/#findComment-185601 Share on other sites More sharing options...
Kitty Posted February 15, 2007 Author Share Posted February 15, 2007 You guys are quick and awesome! Thanks a lot! Quote Link to comment https://forums.phpfreaks.com/topic/38658-counting-files-in-a-folder/#findComment-185603 Share on other sites More sharing options...
utexas_pjm Posted February 15, 2007 Share Posted February 15, 2007 No problem. One thought, I think you meant && $directory_layouts != "index.html" as || $directory_layouts != "index.html") will cause the conditional to return true if $directory_layouts != "index.php" even if $directory_layouts == ".." or ".". I think it was just a mistype, but I thought I'd point it out so no one gets confused. Best, Patrick Quote Link to comment https://forums.phpfreaks.com/topic/38658-counting-files-in-a-folder/#findComment-185605 Share on other sites More sharing options...
Kitty Posted February 15, 2007 Author Share Posted February 15, 2007 Hmm, thanks, but if I use || instead of && my files number increases with 3! ??? Quote Link to comment https://forums.phpfreaks.com/topic/38658-counting-files-in-a-folder/#findComment-185609 Share on other sites More sharing options...
utexas_pjm Posted February 15, 2007 Share Posted February 15, 2007 I'm sorry, I don't think I was clear. Use the code I posted with the "&&", the "II" will implicitley count the "." and "..". Quote Link to comment https://forums.phpfreaks.com/topic/38658-counting-files-in-a-folder/#findComment-185611 Share on other sites More sharing options...
Kitty Posted February 15, 2007 Author Share Posted February 15, 2007 No worries! Thank you very much! Quote Link to comment https://forums.phpfreaks.com/topic/38658-counting-files-in-a-folder/#findComment-185615 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.