Get file size

Posted By: Ian on Jun 19, 2011 in PHP, Snippets
Last modified on November 20th, 2013 at 10:28 am,
<?php
$file  = 'demo.jpeg';
echo filesize($file);
?> 

This function is for use on local files only and will not work on remote files but what you can do is download the file to a temporary folder and then get the size that way.
Also note that file results are returned in bytes so would look something like 176924 which is not easily readable but you can convert it to KB or MB or more rounding the numbers up or down to the amount after the decimal point of your choice.

Try Me

<?php
$file  = 'demo.jpeg';
$bytes = filesize($file);
$kbytes = $bytes / 1024;
$mbytes = $kbytes / 1024; 
echo $file." is equal too:-
"; echo round($bytes, 3)." bytes
"; echo round($kbytes, 2)." kilobyte
"; echo round($mbytes, 3)." megabyte
"; ?>

Why would you use this?

Well imagine you were offering a file for download which you updated often but also wanted to show the user it’s file size this would be ideal as it would change each time the file changed!

AnonymousIan.J.Gough

Did this help you? Please Let me know by commenting below even if it’s just to say “Thanks”

leave a comment

About This Site

Dreams are built from lines of code well it's true!
If you can dream it you can usually code it.

I have learned so much from other people on the Internet and this site is for me to give back some of what i have learned and hope people can now learn from me.
Have fun and surf safely,
Ian.J.Gough

protected by copyscape duplicate content check

Categories


Powered by Banner Bar 125