PHP Get File size detailed

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.