This article mainly and everyone to share PHP to get file size in detail, hope to help everyone.
The size of the file can be obtained through the FileSize function, and the file size is expressed as a number of bytes .
$filename = '/data/webroot/usercode/code/resource/test.txt '; $size = FileSize ($filename);
If you want to convert the units of a file size, you can define your own function to implement it.
function GetSize ($size, $format = ' kb ') { $p = 0; if ($format = = ' kb ') { $p = 1; } elseif ($format = = ' mb ') { $p = 2; } elseif ($format = = ' GB ') { $p = 3; } $size/= Pow (1024x768, $p); Return Number_format ($size, 3);} $filename = '/data/webroot/usercode/code/resource/test.txt '; $size = FileSize ($filename); $size = GetSize ($size, ' KB '); Perform unit conversions echo $size. ' KB ';
It is important to note that there is no simple function to get the size of the directory, the size of the directory is the sum of all subdirectories and the size of the file, so the recursive way to calculate the size of the directory.