I want to enable users to upload some files (pictures) in their own folders. But that should be possible only if that folders contain less than five pictures. If there are 5 pictures already, script has to let know user that his/her folder is full. So, I wonder if there is function in php that count number of files in folder. Or any other way in php to do that? Thanks in advance.
-
1[`count(glob('folder/*.{jpg,png,gif}", GLOB_BRACE)`](http://php.net/manual/de/function.glob.php)? – insertusernamehere Sep 03 '13 at 13:34
-
Possible duplicate of [Count how many files in directory php](http://stackoverflow.com/questions/12801370/count-how-many-files-in-directory-php) – Organic Advocate Dec 10 '16 at 17:45
6 Answers
Use the FilesystemIterator
as shown:
$dir = "/path/to/folder";
$fi = new FilesystemIterator($dir, FilesystemIterator::SKIP_DOTS);
$fileCount = iterator_count($fi);
-
-
@DonCallisto I think I saw this answer on stackoverflow to another similar question, I've used it since and it works well – dops Sep 05 '13 at 00:50
Nothing easier: use opendir()
and readdir()
just like follow:
<?php
$images_extension_array = array("jpg","jpeg","gif","png");
$dir = "/path/to/user/folder";
$dir_resource = opendir($dir);
$file_count = 0;
while (($file = readdir($dir_resource)) !== false) { // scan directory
$extension_from = strrpos($file,"."); // isolate extension index/offset
if ($extension_from && in_array(substr($file,$extension_from+1), $images_extension_array))
$file_count ++; //if has extension and that extension is "associated" with an image, count
}
if ($number_of_files == %) {
//do stuff
}
Obviously this doesn't take into account file extensions...
You can also use:

- 29,419
- 9
- 72
- 100
You could just let PHP find the files for you...then count them.
$count = count(glob("$path_to_user_dir/*"));

- 84,970
- 20
- 145
- 172
-
-
@Pierre: It's quite similar, yes. I was actually about to include some info on how to find only image files until i saw that. Well, that, and before i realized that on *nix, globbing by extension would get iffy. (Case-sensitive OSes and/or file systems wouldn't pick up `whatever.JPG`, for example; you'd have to include each combination of case, or at least the common ones.) – cHao Sep 03 '13 at 13:44
I really like dops answer, but it will return the count of files, directories, and symlinks, which may not be the goal. If you just want a count of the local files in a directory, you can use:
$path = "/path/to/folder";
$fs = new FilesystemIterator($path);
foreach($fs as $file) {
$file->isFile() ? ++$filecount : $filecount;
}

- 36,459
- 25
- 97
- 163
This little function here is a modification to some code I found a little while ago that will also count all of the sub Folders and everything in those folders as well:
<?PHP
$folderCount = $fileCount = 0;
countStuff('.', $fileCount, $folderCount);
function countStuff($handle, &$fileCount, &$folderCount)
{
if ($handle = opendir($handle)) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
if (is_dir($entry)) {
echo "Folder => " . $entry . "<br>";
countStuff($entry, $fileCount, $folderCount);
$folderCount++;
} else {
echo "File => " . $entry . "<br>";
$fileCount++;
}
}
}
closedir($handle);
}
}
echo "<br>==============<br>";
echo "Total Folder Count : " . $folderCount . "<br>";
echo "Total File Count : " . $fileCount;
?>
NOTE: I will also post the original code that will just count the files and folders of the parent directory and not the sub-folders children below:
if ($handle = opendir('.')) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
if (is_dir($entry)) {
echo "Folder => " . $entry . "<br>";
countStuff($entry, $fileCount, $folderCount);
$folderCount++;
} else {
echo "File => " . $entry . "<br>";
$fileCount++;
}
}
}
echo "<br>==============<br>";
echo "Total Folder Count : " . $folderCount . "<br>";
echo "Total File Count : " . $fileCount;
closedir($handle);
}

- 172
- 1
- 11
You can use
$nbFiles=count(scandir('myDirectory'))-2;
(-2 is for removing "." and "..")

- 429
- 3
- 15