This article mainly introduces the method of calculating the size of a file in PHP, involving the skills of operating files in PHP. It has certain reference value and is needed Friends can refer to it
The example in this article describes how PHP calculates the size of a file. Share it with everyone for your reference. The details are as follows:
?
3 4 513 14
15
16
17
18
19
20
21
22
23
|
<🎜>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";<🎜> <🎜>?> |