假设我们有一个图像文件,存储在远程服务器中(例如,让我们采用 此图像),我们如何确定(在 PHP 代码中)它的文件大小?
如果文件位于服务器上,我们将使用文件大小(请参阅此处),但这不适用于远程文件(请参阅此处 )。
另一种选择是检查“内容长度”,但我相信它不适用于图像文件(请参阅此处)
我想要一种像给定的解决方案此处(例如,类似:
<?php
function get_remote_size($url) { // magic
}
echo get_remote_size("http://humus101.com/wp-content/uploads/2009/11/Hummus-soup.jpg");
?>
但不需要下载图像。这可能吗?
Let's say we have an image file, stored in a remote server (for example, let's take this image), how can we determine (in PHP code) it's file size?
If the file was on server, we would have used filesize (see here), but this wouldn't work on a remote file (see here).
The other alternative is to check for the "Content-Length", but I believe it wouldn't work for an image file (see here)
I would like for a solution like the one given here (e.g, something like:
<?php
function get_remote_size($url) { // magic
}
echo get_remote_size("http://humus101.com/wp-content/uploads/2009/11/Hummus-soup.jpg");
?>
But without the need to download the image. Is that possible?
发布评论
评论(3)
假设您担心文件的大小(而不是图像的尺寸),您可以获取 Content-Length 并且通常会起作用。
如果另一端的服务器不提供标头,您将别无选择,只能获取该文件,并在本地检查其大小。
Assuming you're worried about the size of the file (not the dimensions of the image), you can grab Content-Length and it will usually work.
If the server on the other end does't supply the header, you'll have no choice but to GET the file, and check its size locally.
echo get_headers($url,1)['内容长度'];
echo get_headers($url,1)['Content-Length'];
其他人有一个函数,基本上将其作为套接字连接:
http://snippets.dzone.com/posts/show/1207
Someone else has a function for that that basically does it as a socket connection:
http://snippets.dzone.com/posts/show/1207