i have an folder 'files' ok i want know the sum of size of its files
I like this version
Elzo Valugi
2010-05-07 11:11:56
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
2010-05-07 11:36:02
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
2010-05-07 11:45:14
`array_reduce(glob('?*.?*', GLOB_NOSORT), function($sum,$path){return $sum+filesize($path);});` that said, using the DirectoryIterator or a derivative is *much* prettier.
salathe
2010-05-07 13:12:59
+1
A:
Create a loop for all the files in the folder an and use the filesize function.
Elzo Valugi
2010-05-07 11:09:50
+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
2010-05-07 11:21:39
A:
frady
2010-05-23 13:34:25