note 53956 deleted from function.filesize by aidan

From: Date: Wed, 13 Jul 2005 03:17:47 +0000
Subject: note 53956 deleted from function.filesize by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-91860@lists.php.net to get a copy of this message
Note Submitter: Joshua Kaiser <joeboe at gmail dot com> ---- 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.

« previous php.notes (#91860) next »