Forum Moderators: coopster
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
Thanks
-ENIL
There may be pre-made classes for something like this. You can always check the script repositories(hotscripts, sourceforge, etc...) or phpclasses.org
[edit]
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.
[/edit]
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
-ENIL
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';
?>
function directory_size($dir)
/* Return the file size for files in directory
Inputs:
$dirThe directory to check
Outputs:
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);
}
else
{
$retval = $retval + filesize($dir."/".$file);
}
}
}
closedir($dirhandle);
return $retval;
}