Get file size

Posted By: Ian on Jun 19, 2011 in PHP, Snippets
<?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!

Ian

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

Contact Form

captcha

-
blank1
blank2
blank3
blank4
blank5
blank6
blank7
blank8
blank9
blank10
blank11
blank12
blank13
blank14
blank15