2

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.

natttan
  • 119
  • 1
  • 3
  • 11

6 Answers6

9

Use the FilesystemIterator as shown:

$dir = "/path/to/folder";
$fi = new FilesystemIterator($dir, FilesystemIterator::SKIP_DOTS);
$fileCount = iterator_count($fi);
Anthony
  • 36,459
  • 25
  • 97
  • 163
dops
  • 89
  • 2
6

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:

  • scandir() ---> read here
  • FilesystemIterator class (as dops's answer correctly suggest) ---> read here
DonCallisto
  • 29,419
  • 9
  • 72
  • 100
2

You could just let PHP find the files for you...then count them.

$count = count(glob("$path_to_user_dir/*"));
cHao
  • 84,970
  • 20
  • 145
  • 172
  • it's a copy from "insertusernamehere" comment – Pierre Sep 03 '13 at 13:43
  • @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
2

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;
}
Anthony
  • 36,459
  • 25
  • 97
  • 163
1

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);
}
Darksaint2014
  • 172
  • 1
  • 11
0

You can use

$nbFiles=count(scandir('myDirectory'))-2;

(-2 is for removing "." and "..")

Pierre
  • 429
  • 3
  • 15