How can I specify the image file size from the url?

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

$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 that $image_size be formatted with human-readable files, such as “156.8 KB” or “20.1 MB”.

+7
php filesize
source share
5 answers

Use filesize like this:

 echo filesize($filename) . ' bytes'; 

You can also format the size block using 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]); } } 
+25
source share

Images and script are placed on one account (one site).

Then do not use the URL: use the direct path to the file and filesize() .

+3
source share

Note:

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

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

+3
source share
  echo filesize($_SERVER['DOCUMENT_ROOT']."/images/full-1091.jpg"); 

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

as for formatted output ("156.8 KB" or "20.1 MB"), try to help yourself and use the search. There are already a ton of answers to this question.

-one
source share
 <?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 that this might work for you

-5
source share

All Articles