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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3381506/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:32:44  来源:igfitidea点击:

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

phpfilesize

提问by James

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

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

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

How can we get the sizeof this file?

我们如何获得这个文件的大小

Pseudo-code:

伪代码:

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

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

我更喜欢$image_size以人类可读的文件大小进行格式化,例如“156,8 KB”或“20,1 Mbytes”。

回答by Sarfraz

Use filesizefunction like this:

使用这样的filesize功能:

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

回答by Pekka

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

托管在同一帐户(一个站点)上的图像和脚本。

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

然后不要使用 URL:使用直接文件路径和filesize().

回答by Mahdi Jazini

Please Note:

请注意:

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

filesize()函数的结果被缓存了!使用clearstatcache()清除缓存...

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

来源:http: //www.w3schools.com/php/func_filesystem_filesize.asp

回答by Your Common Sense

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

note that http://site.com/images/full-1091.jpgis nota file

请注意,这http://site.com/images/full-1091.jpg不是文件

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.

至于格式化的输出(“156,8 Kbytes”或“20,1 Mbytes”)尝试帮助自己并使用搜索。这个问题已经有很多答案了。

回答by Ankur Mukherjee

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

检查这个它可以为你工作