使用PHP中的URL获取远程文件大小


Get remote file size using URL in PHP

我正在尝试使用获取图像大小

get_headers($url)

但我收到了类似的警告

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

有其他方法吗?

谢谢。

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

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

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

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