使用filesize读取远程文件大小 [英] Reading remote file size using filesize

查看:584
本文介绍了使用filesize读取远程文件大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了 filesize()能够计算文件的手册远程文件的大小. 但是,当我尝试使用下面的代码段进行操作时.我收到错误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用于 文件大小().结案.我会用 摘录此处作为解决方法 解决方案.

解决方案

filesize不适用于HTTP-它取决于stat,该stat

I read the manual that filesize() is able to calculate file size from remote file. However, when I try to do that with snippet below. I got error PHP Warning: filesize(): stat failed for http://someserver/free_wallpaper/jpg/0000122_480_320.jpg in /tmp/test.php on line 5

Here's my snippet:

$file = "http://someserver/free_wallpaper/jpg/0000122_480_320.jpg";
echo filesize( $file );

Turns out, I can't use HTTP for filesize(). Case close. I'll use snippet here as a work-around solution.

解决方案

filesize doesn't work for HTTP - it depends on stat, which isn't supported for the HTTP(S) protocol.

The PHP man page for filesize says:

Refer to List of Supported Protocols/Wrappers for a listing of which wrappers support stat() family of functionality.

This doesn't mean every protocol/wrapper works "out of the box" with filesize. You could always get around that by doing a full GET of the remote URL and calculating the size of the data you get back, of if you can get a Content-Length header you could try a HEAD request to avoid transferring the file data.

这篇关于使用filesize读取远程文件大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆