开发者

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

开发者 https://www.devze.com 2023-01-09 13:58 出处:网络
Images and script are hosted on the same account (one site), but we know开发者_如何学Python only the URL of the image.

Images and script are hosted on the same account (one site), but we know开发者_如何学Python 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".


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]); }
}


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

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


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


 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.


<?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

0

精彩评论

暂无评论...
验证码 换一张
取 消