Home >Backend Development >PHP Tutorial >How to calculate the size of a file in php_PHP tutorial
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";<๐> <๐>?> |