使用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 文件大小()。案件关闭。我会用 代码片段此处作为解决方法 解决方案。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
filesize
不适用于 HTTP - 它取决于stat
,其中 HTTP(S) 协议不支持。文件大小的 PHP 手册页说:
请参阅支持的协议/包装器列表,以获取哪些包装器支持 stat() 系列功能的列表。
这并不意味着每个协议/包装器都“超出”工作范围。框”与
文件大小
。您始终可以通过对远程 URL 进行完整的 GET 并计算返回的数据大小来解决这个问题,如果您可以获得 Content-Length 标头,则可以尝试 HEAD 请求以避免传输文件数据。filesize
doesn't work for HTTP - it depends onstat
, 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.获取 Web url 的文件大小
您可以使用下面的代码function getFileSize($file){
You can get file size of web url using below code
function getFileSize($file){
我不知道你在哪里读到的。
filesize
需要stat
,并基于HTTP 包装器
页面,我认为它在任何版本的 PHP 上都不支持stat
。I don't know where you read that.
filesize
requiresstat
, and based on theHTTP wrapper
page, I don't think it supportsstat
on any version of PHP.您可能想使用 http_head( )。我认为您引用的代码片段会下载文件以确定文件大小,这可能没有必要。如果您引用的 Web 服务器发回准确的标头信息,您应该能够确定文件大小、修改日期和其他一些有用的识别功能。
You might want to investigate using http_head(). I think the snippet you reference downloads the file to determine the file size, which may not be necessary. If the web server you are referencing sends back accurate header information, you should be able to determine file-size, date-modified and several other useful identifying features.
如果您想获取远程图片大小,请尝试使用
getimagesize()
If you want to get the remote picture size, try using
getimagesize()