Home>Article>Backend Development> Detailed explanation of obtaining file size in php
This article mainly shares with you the detailed explanation of how to obtain the file size in php. I hope it can help you.
The size of the file can be obtained through the filesize function. The file size is expressed inbytes.
$filename = '/data/webroot/usercode/code/resource/test.txt'; $size = filesize($filename);
If you want to convert the unit of file size, you can define your own function to achieve 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(1024, $p); return number_format($size, 3); }$filename = '/data/webroot/usercode/code/resource/test.txt';$size = filesize($filename);$size = getsize($size, 'kb'); //进行单位转换echo $size.'kb';
It is worth noting that the size of the directory cannot be obtained through a simple function. The size of the directory is the sum of the sizes of all subdirectories and files in the directory. Therefore, a recursive method is needed to calculate the directory in a loop. the size of.
Related recommendations:
Multiple ways to get file extensions in PHP
How to get file suffixes in PHP
js method to get all the file names in the file is shared
The above is the detailed content of Detailed explanation of obtaining file size in php. For more information, please follow other related articles on the PHP Chinese website!