PHP filesize (original) (raw)
Summary: in this tutorial, you will learn how to use the PHP filesize() function to get the size of a file.
Introduction to the PHP filesize() function #
The filesize() function returns the size of a given file in bytes:
filesize ( string $filename ) : int|falseCode language: PHP (php)
The filesize() function returns the size of the file specified by the $filename in bytes. In case of an error, the function returns false.
PHP filesize() function example #
The following example uses the filesize() function to get the size of the readme.txt file in bytes:
`<?php
$filename = 'readme.txt'; echo filename,′:′,filesize(filename,': ', filesize(filename,′:′,filesize(filename),' bytes';`Code language: HTML, XML (xml)
Output:
readme.txt: 19 bytesCode language: CSS (css)
In practice, you will rarely use the bytes for showing the size of the file. To get the human-readable size of a file, you can use the following function:
`function format_filesize(int bytes,intbytes, int bytes,intdecimals = 2): string { $units = 'BKMGTP'; factor=floor((strlen(factor = floor((strlen(factor=floor((strlen(bytes) - 1) / 3);
return sprintf("%.{$decimals}f", <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>b</mi><mi>y</mi><mi>t</mi><mi>e</mi><mi>s</mi><mi mathvariant="normal">/</mi><mi>p</mi><mi>o</mi><mi>w</mi><mo stretchy="false">(</mo><mn>1024</mn><mo separator="true">,</mo></mrow><annotation encoding="application/x-tex">bytes / pow(1024, </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal">b</span><span class="mord mathnormal" style="margin-right:0.03588em;">y</span><span class="mord mathnormal">t</span><span class="mord mathnormal">es</span><span class="mord">/</span><span class="mord mathnormal">p</span><span class="mord mathnormal">o</span><span class="mord mathnormal" style="margin-right:0.02691em;">w</span><span class="mopen">(</span><span class="mord">1024</span><span class="mpunct">,</span></span></span></span>factor)) . <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>u</mi><mi>n</mi><mi>i</mi><mi>t</mi><mi>s</mi><mo stretchy="false">[</mo><mo stretchy="false">(</mo><mi>i</mi><mi>n</mi><mi>t</mi><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">units[(int)</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal">u</span><span class="mord mathnormal">ni</span><span class="mord mathnormal">t</span><span class="mord mathnormal">s</span><span class="mopen">[(</span><span class="mord mathnormal">in</span><span class="mord mathnormal">t</span><span class="mclose">)</span></span></span></span>factor];} `Code language: PHP (php)
Summary #
- Use the PHP
filesize()function to get the size of a file in bytes.
Did you find this tutorial useful?