Jump to content

Archived

This topic is now archived and is closed to further replies.

booza

display directory

Recommended Posts

how can i display how many files in a directory

ie..

there is currently 701 files

Share this post


Link to post
Share on other sites
[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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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 directory
2. read all the files via a while loop (caveat: remember to exclude '.' and '..')
3. store each file in an array from within the while loop
4. use count() on the array to determine how many elements exist in that array

you 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 :)

Share this post


Link to post
Share on other sites
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 contents
if (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.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.