【问题标题】:Get remote file size using URL in PHP [duplicate]在 PHP 中使用 URL 获取远程文件大小 [重复]
【发布时间】:2013-08-07 01:26:18
【问题描述】:

我正在尝试使用

get_headers($url)

但是我收到了类似的警告

get_headers() [function.get-headers]: http:// wrapper is disabled in the server configuration by allow_url_fopen=0 in

还有其他方法吗?

谢谢。

【问题讨论】:

标签: php


【解决方案1】:

您必须执行 HEAD 请求。这将告诉服务器您只对 HTTP 标头感兴趣。

stream_context_set_default(
    array(
        'http' => array(
            'method' => 'HEAD'
        )
    )
);
$headers = get_headers('http://example.com');

这将返回一个标题数组。您必须找到具有文件大小(字节数)的内容长度项。

【讨论】:

  • 很好的答案,谢谢老兄
【解决方案2】:

您可以使用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;

【讨论】:

    猜你喜欢
    • 2011-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-27
    • 2018-05-08
    • 1970-01-01
    相关资源
    最近更新 更多