I have a folder named files
, how do I determine the sum of size of it's files?
Asked
Active
Viewed 6,736 times
6

John
- 1
- 13
- 98
- 177

Mostafa Elkady
- 5,645
- 10
- 45
- 69
-
3Check this: http://www.phpsnaps.com/snaps/view/sum-of-size-of-files-in-directory – nuqqsa May 07 '10 at 11:06
4 Answers
21
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.
9
echo array_sum(array_map('filesize', glob('*')));

Sjoerd
- 74,049
- 16
- 131
- 175
-
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 May 07 '10 at 11:36
-
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 May 07 '10 at 11:45
-
1`array_reduce(glob('?*.?*', GLOB_NOSORT), function($sum,$path){return $sum+filesize($path);});` that said, using the DirectoryIterator or a derivative is *much* prettier. – salathe May 07 '10 at 13:12
4
Well try this using filesize() to calculate file size while iterating through all the files and sub-directory files.
<?php
function get_dir_size($dir_name){
$dir_size =0;
if (is_dir($dir_name)) {
if ($dh = opendir($dir_name)) {
while (($file = readdir($dh)) !== false) {
if($file !=”.” && $file != “..”){
if(is_file($dir_name.”/”.$file)){
$dir_size += filesize($dir_name.”/”.$file);
}
/* check for any new directory inside this directory */
if(is_dir($dir_name.”/”.$file)){
$dir_size += get_dir_size($dir_name.”/”.$file);
}
}
}
}
}
closedir($dh);
return $dir_size;
}
$dir_name = “directory name here”;
/* 1048576 bytes == 1MB */
$total_size= round((get_dir_size($dir_name) / 1048576),2) ;
print “Directory $dir_name size : $total_size MB”;
?>
0
if you have access to exec function you can use this code:
exec("du {directory_path_you_want_see_size} -s 2>&1",$output);
echo intval($output[0]) ;
example:
exec("du /home/ -s 2>&1",$output);
echo intval($output[0]) ;

mohammad inanloo
- 90
- 1
- 4
-
Please don't recommend the use of `exec()`, especially when it involves any kind of variable, user-supplied input. That just opens up the possibility for shell command injections and all its security-related implications, and nobody really wants that. However, if it cannot be avoided, at least point to [escapeshellarg()](http://php.net/manual/en/function.escapeshellarg.php) or some other way to sanitize user input before it is passed to `exec()`. – Striezel Nov 22 '16 at 21:20