note 53956 added to function.filesize
| From: | JoshuaKaiser | Date: | Sat, 18 Jun 2005 17:35:14 +0000 |
| Subject: | note 53956 added to function.filesize | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-90629@lists.php.net to get a copy of this message | ||
I found this interesting. The recursive function listed below actually reports a different size
than just running "du -sk" from the command line. I'm assuming the "du"
tool is probably more accurate. I was also curious about speed as well so I converted the script
into a CLI script and timed them both. Here is the converted script:
#!/usr/bin/php -q
<?php
function get_folder_size($target, $output=false)
{
$sourcedir = opendir($target);
while(false !== ($filename = readdir($sourcedir)))
{
if($output)
{ echo "Processing: ".$target."/".$filename."<br>"; }
if($filename != "." && $filename != "..")
{
if(is_dir($target."/".$filename))
{
// recurse subdirectory; call of function recursive
$totalsize += get_folder_size($target."/".$filename, $output);
}
else if(is_file($target."/".$filename))
{
$totalsize += filesize($target."/".$filename);
}
}
}
closedir($sourcedir);
return $totalsize;
}
$size = get_folder_size($argv[1],$argv[2]);
// $size has the value of bytes found in directory
// output size in kb
echo "~".(int)($size/1024)."kb\n";
?>
The timed results were as follows:
$ time du -sk Documents
1914008 Documents
real 0m0.695s
user 0m0.090s
sys 0m0.390s
$ time sudo ./foldersize.php Documents
~1792872kb
real 0m1.274s
user 0m0.350s
sys 0m0.590s
As you can see, the du command is nearly twice as fast. I think the most efficient way to get a
folder size is probably just to use the shell_exec() function. I threw together a quick test like
this:
#!/usr/bin/php -q
<?
echo shell_exec('du -sk '.$argv[1]);
?>
Output and time looks like this:
$ time sudo ./phpdu.php Documents/
1914008 Documents/
real 0m0.862s
user 0m0.080s
sys 0m0.480s
As you can see, the overhead from php and the shell_exec() function add a little more time, but it
is still considerably lower.
----
Manual Page -- http://www.php.net/manual/en/function.filesize.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+53956
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+53956&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+53956&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+53956&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+53956&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+53956&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+53956&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+53956&report=yes
Search -- http://master.php.net/manage/user-notes.php