How can I the size of an image file from the URL?
Asked Answered
W

5

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".

Weide answered 1/8, 2010 at 9:43 Comment(0)
T
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]); }
}
Tyrannous answered 1/8, 2010 at 9:50 Comment(1)
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? 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.Tedmann
N
3

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

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

Narceine answered 1/8, 2010 at 9:50 Comment(1)
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: php.net/manual/en/function.filesize.php#92462Tita
S
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

Sethrida answered 29/1, 2014 at 16:19 Comment(0)
L
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.

Libove answered 1/8, 2010 at 9:50 Comment(2)
@Narceine use the file path means "use $_SERVER['DOCUMENT_ROOT']" ?Weide
@Ignatz it's one option - or use the hard-coded full path (/path/to/folder/images/image.jpg)Narceine
N
-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

Nebraska answered 1/8, 2010 at 10:2 Comment(6)
dude, it is ugliest solution I have ever seen.Libove
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 site.com. If the PHP code is running on 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.Ariannearianrhod
@kiamlaluno you can't read. Images and script hosted on the same account.(one site). You'd better limit your hobbies to sci-fi.Libove
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.Ariannearianrhod
@kiam here: 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.Libove
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 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.Ariannearianrhod

© 2022 - 2024 — McMap. All rights reserved.