Jump to content

display directory


booza

Recommended Posts

[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.
Link to comment
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]
Link to comment
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 :)
Link to comment
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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.