Home >Backend Development >PHP Tutorial >How to calculate the size of a file in php, _PHP tutorial
The example in this article describes the method of calculating the size of a file in PHP. Share it with everyone for your reference. The details are as follows:
<?php function dirSize($directoty){ $dir_size=0; if($dir_handle=@opendir($directoty)) { while($filename=readdir($dir_handle)){ $subFile=$directoty.DIRECTORY_SEPARATOR.$filename; if($filename=='.'||$filename=='..'){ continue; }elseif (is_dir($subFile)) { $dir_size+=dirSize($subFile); }elseif (is_file($subFile)){ $dir_size+=filesize($subFile); } } closedir($dir_handle); } return ($dir_size); } $dir_size=dirSize("xym"); echo round($dir_size/pow(1024,1),2)."KB"; ?>
I hope this article will be helpful to everyone’s PHP programming design.