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>
&nbsp;
<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;">&#40;</span><span style="color: #0000ff;">$size</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">// Adapted from: http://www.php.net/manual/en/function.filesize.php</span>
&nbsp;
    <span style="color: #0000ff;">$mod</span> = <span style="color: #cc66cc;">1024</span>;
&nbsp;
    <span style="color: #0000ff;">$units</span> = <a href="http://www.php.net/explode"><span style="color: #000066;">explode</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">' '</span>,<span style="color: #ff0000;">'B KB MB GB TB PB'</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$i</span> = <span style="color: #cc66cc;">0</span>; <span style="color: #0000ff;">$size</span> &gt; <span style="color: #0000ff;">$mod</span>; <span style="color: #0000ff;">$i</span>++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #0000ff;">$size</span> /= <span style="color: #0000ff;">$mod</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> <a href="http://www.php.net/round"><span style="color: #000066;">round</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$size</span>, <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span> . <span style="color: #ff0000;">' '</span> . <span style="color: #0000ff;">$units</span><span style="color: #66cc66;">&#91;</span><span style="color: #0000ff;">$i</span><span style="color: #66cc66;">&#93;</span>;
<span style="color: #66cc66;">&#125;</span>

以上是关于php 人类可读的文件大小的主要内容,如果未能解决你的问题,请参考以下文章

PHP(或jQuery)有没有办法检查字符串是不是是人类可读的?

在 .htaccess 中启用人类可读的 URL

在Java中将字节大小转换为人类可读的格式?

PHP 人类可读文件大小

将骆驼大小写转换为人类可读的字符串?

如何使用 Mahout 成功运行 kmeans 集群(尤其是获得人类可读的输出)