在 PHP 中获取远程文件文件大小的最简单方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1401131/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:25:11  来源:igfitidea点击:

Easiest way to grab filesize of remote file in PHP?

phphttpcurl

提问by meder omuraliev

I was thinking of doing a head request with cURL, was wondering if this is the way to go?

我想用 cURL 做一个 head 请求,想知道这是不是要走的路?

采纳答案by Annika Backstrom

Yes. Since the file is remote, you're completely dependent on the value of the Content-Lengthheader (unless you want to download the whole file). You'll want to curl_setopt($ch, CURLOPT_NOBODY, true)and curl_setopt($ch, CURLOPT_HEADER, true).

是的。由于文件是远程文件,您完全依赖于Content-Length标头的值(除非您想下载整个文件)。你会想要curl_setopt($ch, CURLOPT_NOBODY, true)curl_setopt($ch, CURLOPT_HEADER, true)

回答by softwarefreak

The best solution which follows the KISS principle

遵循KISS原则的最佳解决方案

$head = array_change_key_case(get_headers("http://example.com/file.ext", TRUE));
$filesize = $head['content-length'];

回答by Pascal MARTIN

I'm guessing using curl to send a HEAD request is a nice possibility ; something like this would probably do :

我猜使用 curl 发送 HEAD 请求是一种很好的可能性;这样的事情可能会做:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://sstatic.net/so/img/logo.png');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$size = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
var_dump($size);

And will get you :

并且会让你:

float 3438

This way, you are using a HEAD request, and not downloading the whole file -- still, you depend on the remote server send a correct Content-length header.

这样,您使用的是 HEAD 请求,而不是下载整个文件——仍然依赖于远程服务器发送正确的 Content-length 标头。


Another option you might think about would be to use filesize... But this will fail: the documentation states (quoting):


您可能会想到的另一种选择是使用filesize...但这会失败:文档说明(引用)

As of PHP 5.0.0, this function can also be used with some URL wrappers. Refer to List of Supported Protocols/Wrappersfor a listing of which wrappers support stat() family of functionality.

自 PHP 5.0.0 起,此函数还可与某些 URL 包装器一起使用。有关哪些包装器支持 stat() 系列功能的列表,请参阅支持的协议/包装器列表。

And, unfortunately, with HTTP and HTTPS wrappers, stat()is not supported...

而且,不幸的是,HTTP和HTTPS包装stat()不支持...

If you try, you'll get an error, like this :

如果你尝试,你会得到一个错误,像这样:

Warning: filesize() [function.filesize]: stat failed 
    for http://sstatic.net/so/img/logo.png

Too bad :-(

太糟糕了 :-(

回答by troelskn

Using a HEAD request and checking for Content-Lengthis the standard way to do it, but you can't rely on it in general, since the server might not support it. The Content-Lengthheader is optional, and further the server might not even implement the HEAD method. If you know which server you're probing, then you can test if it works, but as a general solution it isn't bullet proof.

使用 HEAD 请求并检查Content-Length是执行此操作的标准方法,但通常不能依赖它,因为服务器可能不支持它。该Content-Length标题是可选的,进一步的服务器甚至可能不执行HEAD方法。如果您知道您正在探测哪个服务器,那么您可以测试它是否有效,但作为一般解决方案,它不是防弹的。

回答by Alix Axel

If you don't need a bulletproof solution you can just do:

如果您不需要防弹解决方案,您可以这样做:

strlen(file_get_contents($url));