如何从URL中获取图像文件的大小?

Jam*_*mes 7 php filesize

图像和脚本托管在同一个帐户(一个站点)上,但我们只知道图像的URL.

$image = "http://example.com/images/full-1091.jpg"
Run Code Online (Sandbox Code Playgroud)

我们怎样才能得到这个文件的大小

伪代码:

{ do something with $image and get $image_size }
echo $image_size;
Run Code Online (Sandbox Code Playgroud)

我希望$image_size格式化为人类可读的文件大小,如"156,8 Kbytes"或"20,1 Mbytes".

Sar*_*raz 26

使用这样的filesize功能:

echo filesize($filename) . ' bytes';
Run Code Online (Sandbox Code Playgroud)

您还可以使用此功能格式化大小的单位:

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]); }
}
Run Code Online (Sandbox Code Playgroud)