Web Hosting Talk







View Full Version : how to get size of folder using php?


feelexit
08-12-2005, 11:18 AM
I have a folder A, it has many sub folders, each of sub folders has some files in it.

I need to find out the size of the folder. one thing i can do is to search all hte file and files in the sub folders, and get totoal size of all files.

but i dont know how many level of the sub folder gonna be.

is there a better way to get it ? thanx for your help.

laserlight
08-12-2005, 12:23 PM
Check out the PHP Manual on filesize(), chances are the user contributed notes has an example or two on using it to recursively sum the size of all the files and subdirectories in a directory.

tiamak
08-12-2005, 10:38 PM
if you can use exec() function (or similar)
then use exec('du --si -s '.$directory);

error404
08-12-2005, 11:37 PM
function recursive_filesize($dir)
{
if (!($dh = opendir($dir))) return 0;

$total = 0;
while (($file = readdir($dh)) !== false)
{
if ($file != '.' && $file != '..')
{
$file = $dir . '/' . $file;
if (is_dir($file) && is_readable($file) && !is_link($file))
$total += recursive_filesize($file);
else
$total += filesize($file);
}
}
closedir($dh);
return $total;
}


Total size in bytes.

Dashbox
08-16-2005, 07:58 AM
Total size in kb:

<?php
echo number_format((recursive_filesize(getcwd())/1024));
?>