Jump to content


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


get Directory/File info

Recommended Posts

Hey,I'm new to PHP so I'm still learning,but I have a question:
How do you get the number of files in a certain directory and store that into a variable?

And also,what would be the correct way display that variable's data?

Thanks for any help. ^_^

Share this post

Link to post
Share on other sites
Check the manual:

[a href=\"http://www.php.net/dir\" target=\"_blank\"]http://www.php.net/dir[/a]

There is a lot of good info there. Especially the user comments at the bottom.

There are a couple of functions there that will return an array with all the file names and subdirectory names in an array that you can then do a count on to get the number of files in the directory.

To display the value of nearly any variable, you can use either echo or print.

[a href=\"http://www.php.net/echo\" target=\"_blank\"]http://www.php.net/echo[/a]
[a href=\"http://www.php.net/print\" target=\"_blank\"]http://www.php.net/print[/a]

For arrays, use the print_r function:

[a href=\"http://www.php.net/print_r\" target=\"_blank\"]http://www.php.net/print_r[/a]

Share this post

Link to post
Share on other sites
Thanks for the info,that helped. :D

I'm using
$dir = "/ipbfree";
$dh  = opendir($dir);
while (false !== ($filename = readdir($dh))) {
   $ipbfree[] = $filename;

echo count($ipbfree);
to scan the files in the directory ipbfree.Ipbfree is in the same folder as the index.html that is running the script.
But everytime I try to fix it,it doesn't display anything.The number is between [ ],and so it just shoes [].
I am using PHP 4.3.11.Any ideas?

Share this post

Link to post
Share on other sites
Look at the [a href=\"http://www.php.net/glob\" target=\"_blank\"]glob[/a]() function. It's much easier to use.
$ipbfree = glob('/ipbfree');
echo count($ipbfree);


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.