note 106760 modified in function.filesize by danbrown

From: Date: Tue, 06 Dec 2011 18:38:25 +0000
Subject: note 106760 modified in function.filesize by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-184946@lists.php.net to get a copy of this message
Here a little function which provides automated units^^ <?php function calc_size($path,$size=0,$unit='a',$length=2){ if(!is_dir($path)){$size+=filesize($path);} else{ $dir = opendir($path); while($f = readdir($dir)){ if(is_file($path.DS.$f)){$size+=filesize($path.DS.$f);} elseif(is_dir($path.DS.$f) AND $f!='.' AND $f!='..'){$size=calc_size($path.DS.$f,$size,'',0);} } } if($unit==''){return $size;} elseif($unit=='b' XOR $unit=='a' AND strlen($size) < 4){return number_format($size,$length,',','').' <span title="Byte">Byte</span>';} elseif($unit=='kb' XOR $unit=='a' AND strlen($size) < 7){return number_format(($size/1024),$length,',','').' <span title="Kilobyte">KiB</span>';} elseif($unit=='mb' XOR $unit=='a' AND strlen($size) < 10){return number_format(($size/1024/1024),$length,',','').' <span title="Megabyte">MiB</span>';} elseif($unit=='gb' XOR $unit=='a' AND strlen($size) < 13){return number_format(($size/1024/1024/1024),$length,',','').' <span title="Gigabyte">GiB</span>';} elseif($unit=='tb' XOR $unit=='a' AND strlen($size) < 16){return number_format(($size/1024/1024/1024/1024),$length,',','').' <span title="Terabyte">TiB</span>';} elseif($unit=='pb' XOR $unit=='a' AND strlen($size) < 19){return number_format(($size/1024/1024/1024/1024/1024),$length,',','').' <span title="Petabyte">PiB</span>';} elseif($unit=='eb' XOR $unit=='a' AND strlen($size) < 21){return number_format(($size/1024/1024/1024/1024/1024/1024),$length,',','').' <span title="Exabyte">EiB</span>';} elseif($unit=='zb' XOR $unit=='a' AND strlen($size) < 24){return number_format(($size/1024/1024/1024/1024/1024/1024/1024),$length,',','').' <span title="Zettabyte">ZiB</span>';} elseif($unit=='yb' XOR $unit=='a' AND strlen($size) > 24){return number_format(($size/1024/1024/1024/1024/1024/1024/1024/1024),$length,',','').' <span title="Yottabyte">YiB</span>';} } ?> --was-- Here a little function which provides automated units^^ function calc_size($path,$size=0,$unit='a',$length=2){ if(!is_dir($path)){$size+=filesize($path);} else{ $dir = opendir($path); while($f = readdir($dir)){ if(is_file($path.DS.$f)){$size+=filesize($path.DS.$f);} elseif(is_dir($path.DS.$f) AND $f!='.' AND $f!='..'){$size=calc_size($path.DS.$f,$size,'',0);} } } if($unit==''){return $size;} elseif($unit=='b' XOR $unit=='a' AND strlen($size) < 4){return number_format($size,$length,',','').' <span title="Byte">Byte</span>';} elseif($unit=='kb' XOR $unit=='a' AND strlen($size) < 7){return number_format(($size/1024),$length,',','').' <span title="Kilobyte">KiB</span>';} elseif($unit=='mb' XOR $unit=='a' AND strlen($size) < 10){return number_format(($size/1024/1024),$length,',','').' <span title="Megabyte">MiB</span>';} elseif($unit=='gb' XOR $unit=='a' AND strlen($size) < 13){return number_format(($size/1024/1024/1024),$length,',','').' <span title="Gigabyte">GiB</span>';} elseif($unit=='tb' XOR $unit=='a' AND strlen($size) < 16){return number_format(($size/1024/1024/1024/1024),$length,',','').' <span title="Terabyte">TiB</span>';} elseif($unit=='pb' XOR $unit=='a' AND strlen($size) < 19){return number_format(($size/1024/1024/1024/1024/1024),$length,',','').' <span title="Petabyte">PiB</span>';} elseif($unit=='eb' XOR $unit=='a' AND strlen($size) < 21){return number_format(($size/1024/1024/1024/1024/1024/1024),$length,',','').' <span title="Exabyte">EiB</span>';} elseif($unit=='zb' XOR $unit=='a' AND strlen($size) < 24){return number_format(($size/1024/1024/1024/1024/1024/1024/1024),$length,',','').' <span title="Zettabyte">ZiB</span>';} elseif($unit=='yb' XOR $unit=='a' AND strlen($size) > 24){return number_format(($size/1024/1024/1024/1024/1024/1024/1024/1024),$length,',','').' <span title="Yottabyte">YiB</span>';} } http://php.net/manual/en/function.filesize.php

« previous php.notes (#184946) next »