php中有一个系统自带的计算文件大小的函数,就是filesize(),但是这个函数是以字节为单位的,但是在一些情况下,我们需要很直观的了解一个文件大小,就不仅仅需要字节B这个单位了,还需要KB,MB,GB,甚至更大TB,PB,如果碰到一个很大的文件有49957289167B
php中有一个系统自带的计算文件大小的函数,就是filesize(),但是这个函数是以字节为单位的,但是在一些情况下,我们需要很直观的了解一个文件大小,就不仅仅需要字节B这个单位了,还需要KB,MB,GB,甚至更大TB,PB,如果碰到一个很大的文件有49957289167B,大家一看这么一长串的数字后面单位是字节B,还是不知道这个文件的大小是一个什么概念,但是我们把它转换成GB为单位,就是46.53GB。
所以,需要我们自己动手写一个函数来根据文件大小,自动选择统计单位,是的文件大小很直观。
该函数最主要的是根据文件的字节数,判断应当选择的统计单位,也就是说一个文件用某一单位比如MB,那么该文件肯定小于1GB,否则当然要用GB作为单位了,而且文件要大于KB,不然的话得用更小的单位统计。该函数代码如下
//size() 统计文件大小 function size($byte) { if($byte <p> 还有一个辅助函数 round_up(),该函数用来取舍小数点位数的,四舍五入。<br><br>这样我们就能很好额统计任何一个文件的大小了,首先通过系统自带的filesize()函数获得文件的字节数,再用上面的函数换算成适当的单位就可以了,例如:<br>一个迈克尔杰克逊的演唱会文件,喜欢的可以到这里下载: http://ed2ku.com/file/283525/ (比较大,40多GB),首先获得他的字节数是49957289167B,然后用size()函数,获得该文件大小为46.53GB,当然可以根据需要设定取舍小数点位数。</p> <p> </p> <p><strong>参考:http://www.jishutie.net/dev/php/16939.html </strong></p>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version
Chinese version, very easy to use