【问题标题】:PHP Fread won't workPHP Fread 不起作用
【发布时间】:2013-08-02 16:01:55
【问题描述】:

我的脚本在本地主机上总是运行良好。现在我把我所有的图片都移到了另一个网站上,脚本不再起作用了。为什么会这样,我该如何解决?

错误:

警告:filesize(): stat failed for http://data.localsky.net/panel/img/blocked/box_main.gif in C:\xampp\htdocs\Projects\MVC\application\class.security.php 在线15

我调用函数:

baseImg('http://data.localsky.net/panel/img/blocked/box_main.gif', false);

public function baseImg($path, $auto=true) {
    $img_src = $path;
    $imgbinary = fread(fopen($img_src, "r"), filesize($img_src));
    $img_str = base64_encode($imgbinary);

    if ( preg_match('/(?i)msie [1-8]/', $_SERVER['HTTP_USER_AGENT']) ) {

        if($auto) {
            return '<img src="'.$img_scr.'" />';
        } else {
            echo  $img_src;
        }

    } else {

        if($auto) {
            return '<img src="data:image/jpg;base64,'.$img_str.'" />';
        } else {
            return 'data:image/jpg;base64,'.$img_str;
        }

    }
}

【问题讨论】:

  • 也许filesize() 无法处理 URL(?)
  • 将图像放在单独的域中,并用 php 处理它们是一个巨大的性能杀手
  • 你可以使用 file_get_contents 代替

标签: php fopen fread


【解决方案1】:

您不能在 HTTP URL 上使用 filesize()。并非所有协议都提供大小数据,或支持获取它。 filesize() 应该只用于本地文件。支持的协议列在函数的手册页中:http://php.net/filesizehttp://php.net/manual/en/wrappers.http.php

【讨论】:

  • HTTP 有大小数据:Content-Length
  • 是的,但它不被 HTTP 包装器使用。内容长度经常出错,尤其是在动态构建的数据上。
  • 首先它对我来说似乎是一个缺失的功能,但Content-length is frequently wrong, ... 可能是没有实现它的原因。但是,您的答案是正确的:filesize() 支持某些协议包装器,但不支持 http..
【解决方案2】:

你可以试试这个

$imgbinary = file_get_contents($img_src);

【讨论】:

    【解决方案3】:

    filesize 只能用于本地文件。如果您想获取远程文件的文件大小,请使用以下代码:

    <?php
    $remoteFile = 'http://us.php.net/get/php-5.2.10.tar.bz2/from/this/mirror';
    $ch = curl_init($remoteFile);
    curl_setopt($ch, CURLOPT_NOBODY, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); //not necessary unless the file redirects (like the PHP example we're using here)
    $data = curl_exec($ch);
    curl_close($ch);
    if ($data === false) {
      echo 'cURL failed';
      exit;
    }
    
    $contentLength = 'unknown';
    $status = 'unknown';
    if (preg_match('/^HTTP\/1\.[01] (\d\d\d)/', $data, $matches)) {
      $status = (int)$matches[1];
    }
    if (preg_match('/Content-Length: (\d+)/', $data, $matches)) {
      $contentLength = (int)$matches[1];
    }
    
    echo 'HTTP Status: ' . $status . "\n";
    echo 'Content-Length: ' . $contentLength;
    ?>
    

    代码复制自http://php.net/manual/en/function.filesize.php

    【讨论】:

      【解决方案4】:

      正如 MarcB 指出的那样,您不能在远程文件上使用 filesize()。这是一个基于 cURL 的解决方案,我找到了here

      代码:

      function retrieve_remote_file_size($url){
           $ch = curl_init($url);
      
           curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
           curl_setopt($ch, CURLOPT_HEADER, TRUE);
           curl_setopt($ch, CURLOPT_NOBODY, TRUE);
      
           $data = curl_exec($ch);
           $size = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
      
           curl_close($ch);
           return $size;
      }
      

      用法:

      echo retrieve_remote_file_size('http://data.localsky.net/panel/img/blocked/box_main.gif');
      

      输出:

      53
      

      希望这会有所帮助!

      【讨论】:

      • 在这种情况下,不需要先做一个http请求来获取文件大小,然后再执行另一个来获取数据。
      猜你喜欢
      • 2012-08-25
      • 2022-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-03
      • 2012-08-30
      • 2017-05-14
      • 2017-03-11
      相关资源
      最近更新 更多