php 人类可读的文件大小
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 人类可读的文件大小相关的知识,希望对你有一定的参考价值。
<span style="color: #808080; font-style: italic;">// A much better and accurate version can be found</span>
<span style="color: #808080; font-style: italic;">// in Aidan's PHP Repository: </span>
<span style="color: #808080; font-style: italic;">// http://aidanlister.com/repos/v/function.size_readable.php</span>
<span style="color: #808080; font-style: italic;">/**
* Returns a human readable filesize
*
* @author wesman20 (php.net)
* @author Jonas John
* @version 0.3
* @link http://www.jonasjohn.de/snippets/php/readable-filesize.htm
*/</span>
<span style="color: #000000; font-weight: bold;">function</span> HumanReadableFilesize<span style="color: #66cc66;">(</span><span style="color: #0000ff;">$size</span><span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span>
<span style="color: #808080; font-style: italic;">// Adapted from: http://www.php.net/manual/en/function.filesize.php</span>
<span style="color: #0000ff;">$mod</span> = <span style="color: #cc66cc;">1024</span>;
<span style="color: #0000ff;">$units</span> = <a href="http://www.php.net/explode"><span style="color: #000066;">explode</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">' '</span>,<span style="color: #ff0000;">'B KB MB GB TB PB'</span><span style="color: #66cc66;">)</span>;
<span style="color: #b1b100;">for</span> <span style="color: #66cc66;">(</span><span style="color: #0000ff;">$i</span> = <span style="color: #cc66cc;">0</span>; <span style="color: #0000ff;">$size</span> > <span style="color: #0000ff;">$mod</span>; <span style="color: #0000ff;">$i</span>++<span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span>
<span style="color: #0000ff;">$size</span> /= <span style="color: #0000ff;">$mod</span>;
<span style="color: #66cc66;">}</span>
<span style="color: #b1b100;">return</span> <a href="http://www.php.net/round"><span style="color: #000066;">round</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$size</span>, <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">)</span> . <span style="color: #ff0000;">' '</span> . <span style="color: #0000ff;">$units</span><span style="color: #66cc66;">[</span><span style="color: #0000ff;">$i</span><span style="color: #66cc66;">]</span>;
<span style="color: #66cc66;">}</span>
以上是关于php 人类可读的文件大小的主要内容,如果未能解决你的问题,请参考以下文章