我阅读了文件大小()能够从远程文件中计算文件大小的手册。但是,当我尝试用下面的片段来做这件事的时候。我得到了错误PHP Warning: filesize(): stat failed for http://someserver/free_wallpaper/jpg/0000122_480_320.jpg in /tmp/test.php on line 5
这是我的片段:
$file = "http://someserver/free_wallpaper/jpg/0000122_480_320.jpg";
echo filesize( $file );结果,我不能将HTTP用于文件大小()。案子接近了。我将使用代码片段这里作为解决方案。
发布于 2010-07-12 04:25:07
filesize不适用于HTTP-它依赖于stat,哪个不支持HTTP(S)协议。
文件大小的PHP手册页面上写着:
Refer to List of Supported Protocols/Wrappers for a listing of which wrappers support stat() family of functionality.
这并不意味着每个协议/包装器都使用filesize“开箱即用”。您可以通过完全获取远程URL并计算返回数据的大小来解决这个问题,如果可以获得内容长度的标题,则可以尝试HEAD请求以避免传输文件数据。
发布于 2014-02-26 10:32:16
您可以使用以下代码获取web url的文件大小
函数getFileSize($file){
$ch = curl_init($file);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$data = curl_exec($ch);
curl_close($ch);
$contentLength=0;
if (preg_match('/Content-Length: (\d+)/', $data, $matches)) {
$contentLength = (int)$matches[1];
}
return $contentLength; }发布于 2010-07-12 04:24:58
我不知道你在哪里读到的。filesize需要stat,基于HTTP wrapper页面,我认为它不支持任何版本的HTTP wrapper。
https://stackoverflow.com/questions/3225852
复制相似问题