Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

space used by directory script

i want to know about script that shows used space



3:55 am on Nov 27, 2006 (gmt 0)

5+ Year Member

hi 2 all,

i want to know about such a script that allow me to see the used space on the server of certain folders or directories.

suppose i have 'test' directory on my www.domain.com/
then i want php to show how much this dir has used space

i used disk_total_space() function but it shows whole server space

pls anyone knows this then let me know



11:59 am on Nov 27, 2006 (gmt 0)

WebmasterWorld Senior Member eelixduppy is a WebmasterWorld Top Contributor of All Time 5+ Year Member

I'm thinking that the only way to do this is to loop through the directory and use filesize [us3.php.net] to sum up the size of each file.

There may be pre-made classes for something like this. You can always check the script repositories(hotscripts, sourceforge, etc...) or phpclasses.org

Here's one [phpclasses.org] I found real quick. I have never used it, but if it saves you time you might want to look into it.


1:50 am on Nov 28, 2006 (gmt 0)

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

assuming a unix-like system you could possibly use the du command.

try something like du -sk /directory/name


4:15 am on Nov 28, 2006 (gmt 0)

5+ Year Member

thanks a lot for your reply...

for phranque:
try something like du -sk /directory/name
but how can i use this commaand in php?

for eelixduppy:
thanks for this url
i have downloaded it
it's gr8 trick but require some lines of code.
i m in search of just one or two lines code.
if i will not get it then i have to do as you said...

Thanks again


10:28 am on Nov 28, 2006 (gmt 0)

WebmasterWorld Senior Member swa66 is a WebmasterWorld Top Contributor of All Time 10+ Year Member

To get to a unix command from php:

system(): [php.net...]

To write the function yourself:

define a function.

In the function, open the directory with
opendir() [php.net...]

next iterate in a loop over all the entries in that directory

skip over the . and .. entries

add their size and if you see a subdir, you can recursively call this function again ...

close the directory

return the accumulated size to the caller.

There's plenty of samples contributed on the opendir page to get you started.

The command to get to the size of a file or directory (directories do take up diskspace for themselves too!)
is stat() [php.net...] it returns an array of which the [7] is the size in bytes.

Actually, the stat page referenced above has a fully done script ready to be picked up:
function dir_size($dir)
$handle = opendir($dir);

while ($file = readdir($handle)) {
if ($file!= '..' && $file!= '.' &&!is_dir($dir.'/'.$file)) {
$mas += filesize($dir.'/'.$file);
} else if (is_dir($dir.'/'.$file) && $file!= '..' && $file!= '.') {
$mas += dir_size($dir.'/'.$file);
return $mas;
echo dir_size('DIRECTORIO').' Bytes';


2:48 pm on Nov 28, 2006 (gmt 0)

10+ Year Member

Here's what I use.

function directory_size($dir)
/* Return the file size for files in directory
$dirThe directory to check
The total file size of all files in $dir
$retval = 0;
$dirhandle = opendir($dir);
while ($file = readdir($dirhandle))
if ($file!="." && $file!="..")
if (is_dir($dir."/".$file))
$retval = $retval + directory_size($dir."/".$file);
$retval = $retval + filesize($dir."/".$file);
return $retval;

Featured Threads

Hot Threads This Week

Hot Threads This Month