使用 PHP 中的 URL 获取远程文件大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18073406/
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
Get remote file size using URL in PHP
提问by Manish Chauhan
I am trying to get image size using
我正在尝试使用
get_headers($url)
But I am getting warning like
但我收到警告
get_headers() [function.get-headers]: http:// wrapper is disabled in the server configuration by allow_url_fopen=0 in
Is there any other way to Do so?
有没有其他方法可以这样做?
Thanks.
谢谢。
回答by Manigandan Arjunan
You can use curl_getinfo()
function to get the remote file size.
您可以使用curl_getinfo()
函数来获取远程文件大小。
$ch = curl_init('http://www.google.co.in/images/srpr/logo4w.png');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_NOBODY, TRUE);
$data = curl_exec($ch);
$size = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
curl_close($ch);
echo $size;
回答by Webberig
You must perform a HEAD request. This will tell the server that you're only interested in the HTTP headers.
您必须执行 HEAD 请求。这将告诉服务器您只对 HTTP 标头感兴趣。
stream_context_set_default(
array(
'http' => array(
'method' => 'HEAD'
)
)
);
$headers = get_headers('http://example.com');
This will return an array of headers. You must find the content-length item which has the file size (number of bytes).
这将返回一个标题数组。您必须找到具有文件大小(字节数)的内容长度项。