Jump to content

Archived

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

extrovertive

Counting number of files in a folder?

Recommended Posts

I'm writing a script that allows user to upload their pics to a folder (each user has his/her own folder). That folder will contain only pics. Now, I want it so that I can count the number of files are in the folder.
Is there a function for this? Sort of like count() but for directory?

Share this post


Link to post
Share on other sites
$dir    = '/dir'; //folder to scan
$files = scandir($dir); //scans the folder
$count_of_files = count($files) - 2; // - 2 to account for "." and ".."

*edit*
This is for PHP5. What version are you running?

Share this post


Link to post
Share on other sites
PHP 4 version

$dir = "/dir";
$dh  = opendir($dir);
while (false !== ($filename = readdir($dh))) {
  $files[] = $filename;
}

print_r($files);

Share this post


Link to post
Share on other sites
If you're running version 4.3 or higher, take a look at the glob() function http://www.php.net/glob

[code]<?php
echo count(glob('/dir/*'));
?>[/code]

Ken

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.