Home > Backend Development > PHP Tutorial > Detailed explanation of obtaining file size in php

Detailed explanation of obtaining file size in php

小云云
Release: 2023-03-22 22:04:01
Original
6057 people have browsed it

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 in bytes.

$filename = '/data/webroot/usercode/code/resource/test.txt';
$size = filesize($filename);
Copy after login

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';
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template