homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

space used by directory script
i want to know about script that shows used space

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

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)

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)

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)

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)

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)

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;

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved