tags:

views:

78

answers:

5

i have an folder 'files' ok i want know the sum of size of its files

+1  A: 

see this link for your question

Karthik
+6  A: 
echo array_sum(array_map('filesize', glob('*')));
Sjoerd
I like this version
Elzo Valugi
that doesn't exclude directories, does it? It's also inefficient. First you glob the filenames, then you replace them with their size and then you sum them. That's two additional iterations over the initial array when you could do it all in one iteration.
Gordon
Also, you could add `GLOB_NOSORT` when you don't care for the file order anyway. See http://www.phparch.com/2010/04/28/putting-glob-to-the-test/ - that should add some speed to glob. Still not optimal though.
Gordon
`array_reduce(glob('?*.?*', GLOB_NOSORT), function($sum,$path){return $sum+filesize($path);});` that said, using the DirectoryIterator or a derivative is *much* prettier.
salathe
+1  A: 

Create a loop for all the files in the folder an and use the filesize function.

Elzo Valugi
+3  A: 

With DirectoryIterator and SplFileInfo

$totalSize = 0;
foreach( new DirectoryIterator('/path/to/dir') as $file) {
    if($file->isFile()) {
        $totalSize += $file->getSize();
    }
}
echo $totalSize;

and in case you need that including subfolders:

$iterator = new RecursiveIteratorIterator(
    new RecursiveDirectoryIterator('/path/to/dir'));

$totalSize = 0;
foreach($iterator as $file) {
    $totalSize += $file->getSize();
}
echo $totalSize;

And you can run $totalSize through the code we gave you to format 6000 to 6k for a more human readable output. You'd have to change all 1000s to 1024 though.

Gordon
A: 
frady