9

Images and script are hosted on the same account (one site), but we know only the URL of the image.

$image = "http://example.com/images/full-1091.jpg"

How can we get the size of this file?

Pseudo-code:

{ do something with $image and get $image_size }
echo $image_size;

I would prefer $image_size to be formatted in human-readable file sizes, like "156,8 Kbytes" or "20,1 Mbytes".

TRiG
  • 10,148
  • 7
  • 57
  • 107
James
  • 42,081
  • 53
  • 136
  • 161

5 Answers5

30

Use filesize function like this:

echo filesize($filename) . ' bytes';

You can also format the unit of the size with this function:

function format_size($size) {
      $sizes = array(" Bytes", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB");
      if ($size == 0) { return('n/a'); } else {
      return (round($size/pow(1024, ($i = floor(log($size, 1024)))), 2) . $sizes[$i]); }
}
Sarfraz
  • 377,238
  • 77
  • 533
  • 578
  • Just to be pedantic, this answer perpetuates the confusion between decimal and binary abbreviations for multiples. Is a megabyte 1,000,000 bytes or 1,048,576 bytes? https://en.wikipedia.org/wiki/Binary_prefix I'd suggest either changing $sizes to array(" Bytes", " KiB", " MiB", " GiB", " TiB", " PiB", " EiB", " ZiB", " YiB"), or changing the two occurrences of 1024 to 1000. – RenniePet Jan 06 '18 at 06:05
3

Please Note:

The result of filesize() function are cached! Use clearstatcache() to clear the cache...

source: http://www.w3schools.com/php/func_filesystem_filesize.asp

Mahdi Jazini
  • 791
  • 9
  • 16
3

Images and script hosted on the same account (one site).

Then don't use a URL: Use the direct file path and filesize().

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • I would say that this is the correct solution for files local to the same server as a solution for looking up files via url is quite a bit uglier and longer so it should only be used for remote files: http://www.php.net/manual/en/function.filesize.php#92462 – Ashaman Aug 01 '10 at 10:08
0
 echo filesize($_SERVER['DOCUMENT_ROOT']."/images/full-1091.jpg");

note that http://site.com/images/full-1091.jpg is not a file

as for the formatted output ("156,8 Kbytes" or "20,1 Mbytes") try to help yourself and use search. There is a ton of answers to this question already.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
-4
<?php
  $contents=file_get_contents("http://site.com/images/full-1091.jpg");
   // var_dump($contents);
//  $fp =fopen("1.jpeg","w");
  file_put_contents("1.jpeg",$contents);
  $size= filesize("1.jpeg");
  echo $size."in Bytes";
  ?>

Check this it can work for you

Ankur Mukherjee
  • 3,777
  • 5
  • 32
  • 39
  • 2
    dude, it is ugliest solution I have ever seen. – Your Common Sense Aug 01 '10 at 10:15
  • The solution would be correct (except it is saving the content in 1.jpeg, and trying to get the file size of 1.gif), if the PHP code is running is a server that is different from http://site.com. If the PHP code is running on http://site.com too, then it should directly call `filesize()` passing the image file path. As the OP didn't report if the PHP code is running on the same server that contains the image, this solution is valid too. – apaderno Aug 01 '10 at 21:22
  • @kiamlaluno you can't read. `Images and script hosted on the same account.(one site).` You'd better limit your hobbies to sci-fi. – Your Common Sense Aug 02 '10 at 10:52
  • Col. Shrapnel: I am wearing eyeglasses; what is your excuse to answer to a question with "try to help yourself and use search"? Then, the debate of a file not being a file doesn't make entirely sense, if we are not talking of a resource accessed through HTTP protocol. – apaderno Aug 02 '10 at 11:20
  • @kiam here: http://stackoverflow.com/faq `Please do look around to see if your question has already been asked (and maybe even answered!) before you ask.` And talking of questions, a developer who cannot tell what is a file, is good for nothing. To understand basic principles is way more important than ability to copy and paste a code cnippet someone wrote for you on SO. For what most of you, SO users, take programming for. – Your Common Sense Aug 02 '10 at 12:58
  • I don't know if the OP understands what a file; the only thing I can say it doesn't make sense to access a file locale to the server using http://example.com/filename.extension. I think that who asks a question deserves an explanation of why he is doing the wrong thing; simply saying that is wrong without to exactly explain why it is wrong it is like to copy a code snippet without to know what it does. – apaderno Aug 02 '10 at 13:15