PHP代码统计文件大小(自动确定单位)
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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Validator can be created by adding the following two lines in the controller.
