php 获取图片的 KB 大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3805253/
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
php get the KB size of an image
提问by Macao
i've been googleing but all i get is getimagesize and filesize.
getimagesize dosent get the KB size just width and height which is not what im looking for.
filesize give me the message Warning: filesize() [function.filesize]: stat failed for
the file in question is 51kb .jpg file
我一直在谷歌搜索,但我得到的只是 getimagesize 和 filesize。
getimagesize dosent 获取 KB 大小只是宽度和高度,这不是我要找的。
filesize 给我消息警告:filesize() [function.filesize]: stat failed for
the file is 51kb .jpg file
$imgsize=filesize("http://localhost/projects/site/schwe/user/1/1.jpg");
does not work,
不起作用,
how do i accomplish this?
我如何做到这一点?
采纳答案by Stewie
You cannot get file size of remote elements, either give a relative path on your system OR do a file_get_contents()to get contents first
. Thus, instead of http:// , do a filesize('/path/to/local/system'). Make sure its readable by php process
您无法获取远程元素的文件大小,要么在系统上提供相对路径,要么file_get_contents()先获取内容。因此,代替 http:// ,执行filesize('/path/to/local/system'). 确保它可以被 php 进程读取
回答by BBonifield
You can't look up the filesize of a remote file like that. It is meant for looking at the filesize of local files.
你不能像那样查找远程文件的文件大小。它用于查看本地文件的文件大小。
For instance...
例如...
$imgsize = filesize( '/home/projects/site/1.jpg' );
回答by Quasipickle
filesize()is the function to use. It might be failing because
filesize()是要使用的函数。它可能会失败,因为
- You're trying to get a web address & URL wrappers may not be turned on
- That URL isn't valid.
- 您正在尝试获取网址,并且网址包装器可能未打开
- 该网址无效。
If you're trying to run filesize()on a local file, reference the file system path, not some web URL.
如果您尝试filesize()在本地文件上运行,请引用文件系统路径,而不是某个 Web URL。
回答by Kai23
Or you can also do something like :
或者您也可以执行以下操作:
$header = get_headers($url);
foreach ($header as $head) {
if (preg_match('/Content-Length: /', $head)) {
$size = substr($head, 15);
}
}
回答by Mizbah Ahsan
I had the same problem, which i solved like this. I don't know how optimal it is, but it works for me:
我有同样的问题,我是这样解决的。我不知道它有多优化,但它对我有用:
getimagesize("http://localhost/projects/site/schwe/user/1/1.jpg");
$file_size = $file[0]*$file[1]*$file["bits"];
回答by codaddict
filesizetakes the name of the file as argument not a URL and it returns the size of the file in bytes. You can divide the return value with 1024to get the size in KB.
filesize将文件名作为参数而不是 URL,它以字节为单位返回文件的大小。您可以将返回值与1024相除以获得以 KB 为单位的大小。

