Jump to content


Photo

display directory


  • Please log in to reply
4 replies to this topic

#1 booza

booza
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 05 June 2006 - 12:47 PM

how can i display how many files in a directory

ie..

there is currently 701 files

#2 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 05 June 2006 - 02:00 PM

$num=count(scandir($dir);
echo("There are currently ".$num." files in ".$dir);

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.
Think you're smarty?

(Gone until 20 to November)

#3 booza

booza
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 06 June 2006 - 09:16 PM

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) View Post[/div][div class=\'quotemain\'][!--quotec--]
$num=count(scandir($dir);
echo("There are currently ".$num." files in ".$dir);

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]


#4 Koobi

Koobi
  • Staff Alumni
  • Advanced Member
  • 419 posts
  • LocationColombo, Sri Lanka | South Asia

Posted 06 June 2006 - 09:29 PM

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

#5 nogray

nogray
  • Members
  • PipPipPip
  • Advanced Member
  • 930 posts
  • LocationSan Francisco CA

Posted 06 June 2006 - 09:51 PM

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

<?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)";
?> 

make sure the dir is the correct path.

NoGray.com





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users