booza Posted June 5, 2006 Share Posted June 5, 2006 how can i display how many files in a directoryie..there is currently 701 files Quote Link to comment https://forums.phpfreaks.com/topic/11232-display-directory/ Share on other sites More sharing options...
Orio Posted June 5, 2006 Share Posted June 5, 2006 [code]$num=count(scandir($dir);echo("There are currently ".$num." files in ".$dir);[/code]But scandir requires PHP 5 or higher.So if you have PHP<5, go to:[a href=\"http://www.php.net/manual/en/function.scandir.php\" target=\"_blank\"]http://www.php.net/manual/en/function.scandir.php[/a]And see user's notes, there you can find a function like scandir for PHP<5.Orio. Quote Link to comment https://forums.phpfreaks.com/topic/11232-display-directory/#findComment-42016 Share on other sites More sharing options...
booza Posted June 6, 2006 Author Share Posted June 6, 2006 cant get this to work[!--quoteo(post=380199:date=Jun 5 2006, 09:00 AM:name=Orio)--][div class=\'quotetop\']QUOTE(Orio @ Jun 5 2006, 09:00 AM) [snapback]380199[/snapback][/div][div class=\'quotemain\'][!--quotec--][code]$num=count(scandir($dir);echo("There are currently ".$num." files in ".$dir);[/code]But scandir requires PHP 5 or higher.So if you have PHP<5, go to:[a href=\"http://www.php.net/manual/en/function.scandir.php\" target=\"_blank\"]http://www.php.net/manual/en/function.scandir.php[/a]And see user's notes, there you can find a function like scandir for PHP<5.Orio.[/quote] Quote Link to comment https://forums.phpfreaks.com/topic/11232-display-directory/#findComment-42589 Share on other sites More sharing options...
Koobi Posted June 6, 2006 Share Posted June 6, 2006 it would be helpful if you mentioned what exactly you tried and what error messages you recieved.as Orio pointed out, you would need PHP5 for that particular function.if you're on PHP4.x, the idea is:1. open a directory2. read all the files via a while loop (caveat: remember to exclude '.' and '..')3. store each file in an array from within the while loop4. use count() on the array to determine how many elements exist in that arrayyou don't really have to store the items in an array, you can even have a counter in the while loop.when you say "files" i'm assuming you mean directories as well because on Unix/Linux, directories are also files but if that's not your intent, look into the is_dir() function.i'm assuming you have a general knowledge of file handling in PHP so i've left out some obvious things in my post. if you need further clarification on this matter, let us know...but really, if you had followed Orion's advice in his first post, you would have sorted this problem out...which makes me wonder why i have to type all this :) Quote Link to comment https://forums.phpfreaks.com/topic/11232-display-directory/#findComment-42598 Share on other sites More sharing options...
nogray Posted June 6, 2006 Share Posted June 6, 2006 if you have php5, you'll be missing a ) in this like$num=count(scandir($dir);change it to $num=count(scandir($dir));if you dont, then you'll need to go through the directory and count the files. You can go to [a href=\"http://us3.php.net/manual/en/function.opendir.php\" target=\"_blank\"]http://us3.php.net/manual/en/function.opendir.php[/a] for more details regarding the open directory function, and here is a small example to count the files[code]<?php$dir = "/etc/php5/";$num_of_files = 0;// Open a known directory, and proceed to read its contentsif (is_dir($dir)) { if ($dh = opendir($dir)) { while (($file = readdir($dh)) !== false) { if (is_file($dir . $file)) $num_of_files++; } closedir($dh); }}echo "There are $num_of_files file(s)";?> [/code]make sure the dir is the correct path. Quote Link to comment https://forums.phpfreaks.com/topic/11232-display-directory/#findComment-42606 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.