PHPでファイルサイズを表示するときの条件分岐

NO IMAGE

したいこと

KB/MB/GBで表示分ける

ソースコード

<span class="nv">$filesize</span> <span class="o">=</span> <span class="nb">filesize</span><span class="p">(</span><span class="s2">"/hogehoge.txt"</span><span class="p">);</span>

<span class="k">if</span> <span class="p">(</span><span class="nv">$filesize</span> <span class="o"><=</span> <span class="p">(</span><span class="mi">1024</span><span class="o">**</span><span class="mi">2</span><span class="p">)){</span><span class="c1">// KB</span>
  <span class="nv">$filesize</span> <span class="o">=</span> <span class="nv">$filesize</span><span class="o">/</span><span class="mi">1024</span><span class="p">;</span>
  <span class="nv">$filesize</span> <span class="o">=</span> <span class="nb">number_format</span><span class="p">(</span><span class="nv">$filesize</span><span class="p">,</span><span class="mi">2</span><span class="p">);</span>
  <span class="nv">$filesize</span> <span class="o">.=</span> <span class="s1">'KB'</span><span class="p">;</span>
<span class="p">}</span><span class="k">elseif</span><span class="p">(</span><span class="nv">$filesize</span> <span class="o"><=</span> <span class="p">(</span><span class="mi">1024</span><span class="o">**</span><span class="mi">3</span><span class="p">)){</span> <span class="c1">// MB</span>
  <span class="nv">$filesize</span> <span class="o">=</span> <span class="nv">$filesize</span><span class="o">/</span><span class="p">(</span><span class="mi">1024</span><span class="o">**</span><span class="mi">2</span><span class="p">);</span>
  <span class="nv">$filesize</span> <span class="o">=</span> <span class="nb">number_format</span><span class="p">(</span><span class="nv">$filesize</span><span class="p">,</span><span class="mi">2</span><span class="p">);</span>
  <span class="nv">$filesize</span> <span class="o">.=</span> <span class="s1">'MB'</span><span class="p">;</span>
<span class="p">}</span><span class="k">elseif</span><span class="p">(</span><span class="nv">$filesize</span> <span class="o"><=</span> <span class="p">(</span><span class="mi">1024</span><span class="o">**</span><span class="mi">4</span><span class="p">)){</span> <span class="c1">//GB</span>
  <span class="nv">$filesize</span> <span class="o">=</span> <span class="nv">$filesize</span><span class="o">/</span><span class="p">(</span><span class="mi">1024</span><span class="o">**</span><span class="mi">3</span><span class="p">);</span>
  <span class="nv">$filesize</span> <span class="o">=</span> <span class="nb">number_format</span><span class="p">(</span><span class="nv">$filesize</span><span class="p">,</span><span class="mi">2</span><span class="p">);</span>
  <span class="nv">$filesize</span> <span class="o">.=</span> <span class="s1">'GB'</span><span class="p">;</span>
<span class="p">}</span><span class="k">else</span><span class="p">{</span> <span class="c1">//Others</span>
  <span class="nv">$filesize</span> <span class="o">=</span> <span class="nv">$filesize</span><span class="o">/</span><span class="p">(</span><span class="mi">1024</span><span class="o">**</span><span class="mi">4</span><span class="p">);</span>
  <span class="nv">$filesize</span> <span class="o">=</span> <span class="nb">number_format</span><span class="p">(</span><span class="nv">$filesize</span><span class="p">,</span><span class="mi">2</span><span class="p">);</span>
  <span class="nv">$filesize</span> <span class="o">.=</span> <span class="s1">'TB'</span><span class="p">;</span>
<span class="p">}</span>

やってること

  1. filesizeでファイルサイズを取得し、$filesizeに格納(このときはByteで取得される)
  2. $filesizeを1024のn乗(n>=2)と比較していく
  3. n乗より小さければn-1乗の単位を与えるような条件分岐(このときに小数点以下2桁で丸める)

感想

PHPで累乗を**で書けることを知ったが、これがめっちゃ楽。

<span class="c1">//今まで</span>
<span class="nv">$a</span> <span class="o">*</span> <span class="nv">$a</span>

<span class="c1">//これから</span>
<span class="nv">$a</span> <span class="o">**</span> <span class="mi">2</span>

累乗が5乗とか10乗になった時に間違える心配が減る。

また、この条件分岐はファイル一覧に組み合わせているが、ほかにも使う機会も多そうなので楽に書けて良かった。