代码之家  ›  专栏  ›  技术社区  ›  ariefbayu

使用文件大小读取远程文件大小

  •  2
  • ariefbayu  · 技术社区  · 16 年前

    我读了手册 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 FielZeIe()。情况接近。我会用的 片断 here 作为一项工作 解决方案。

    5 回复  |  直到 12 年前
        1
  •  2
  •   Matthew Iselin    16 年前

    filesize 不适用于HTTP-这取决于 stat 哪一个 isn't supported for the HTTP(S) protocol .

    文件大小的php手册页显示:

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

    这并不意味着每个协议/包装器都可以“开箱即用”地 文件大小 . 通过对远程URL进行完全获取并计算返回的数据的大小,您总是可以绕过这一点,如果您可以获得一个Content-Length头,则可以尝试头请求以避免传输文件数据。

        2
  •  4
  •   Mehul Dudhat    12 年前

    您可以使用以下代码获取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; }
    
        3
  •  0
  •   Matthew Flaschen    16 年前

    我不知道你在哪里读到的。 filesize 要求 stat ,并基于 HTTP wrapper 佩奇,我认为它不支持 斯达 在任何版本的PHP上。

        4
  •  0
  •   Andrew    16 年前

    你可能想调查使用 http_head() . 我认为您引用的代码片段下载文件以确定文件大小,这可能不是必需的。如果您所引用的Web服务器返回准确的头信息,那么您应该能够确定文件大小、修改日期以及其他一些有用的标识功能。

        5
  •  0
  •   Rob Hruska MegalomanINA    13 年前

    如果要获取远程图片大小,请尝试使用 getimagesize()