【问题标题】:PHP readfile errorPHP读取文件错误
【发布时间】:2016-06-01 10:52:28
【问题描述】:

我收到一个错误。我正在尝试阅读附件。它在大多数文件上都可以正常工作,但在少数文件中我得到了这个错误。这些文件具有相同的格式,并且它尝试读取的位置是正确的。我已经在 windows 资源管理器上测试过了。这是我正在阅读的方式:

        header('Content-Description: File Transfer'); 
        header('Content-Type: application/octet-stream'); 
        header('Content-Disposition: attachment; filename="' . $filename .'"'); 
        header('Content-Transfer-Encoding: binary'); 
        header('Expires: 0'); 
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
        header('Pragma: public'); 
        header('Content-Length: ' . filesize($attachment_location)); 
        ob_clean(); 
        flush(); 
        readfile($attachment_location);     
        exit();  

这是我得到的错误

警告:readfile(C:\Users\Public\asdgasd\4sf3\Suppliers\saf342\Files\Revit\2016\Seinätikas.rfa): failed to open stream: No such file or directory in C:\xampp\htdocs\web\downloadattachment.php on line 58

【问题讨论】:

  • 它清楚地表明你的文件系统中没有这样的文件。如果存在这样的文件,请在调用 readfile() 之前检查它。
  • 我确实检查过它确实存在。
  • 不,我的意思是你要做这样的事情:var_dump(file_exists($attachment_location));这会告诉你php是否看到这个文件。
  • 响应是:bool(false) 所以php看不到。
  • 元音变音、Windows 和 PHP?致命的组合。 :)

标签: php readfile


【解决方案1】:

作为 Johndodo 类型的引用,您需要确保 PHP 使用正确的内部字符集和编码进行操作,以便它识别文件在您的 (windows) 目录结构中的存储方式。查看character set your windows system is using 的内容,然后为PHP internal encoding 使用相同的字符集。

编辑:

逻辑过程是:

  • 从电子邮件中打开文件引用并将convert the filename $var 转换为正确的编码。
  • 检查file_exists
  • 继续将$var 传递给readfile 函数以打开。

【讨论】:

    【解决方案2】:

    能否请您提供$filename 和$attachment_location 的内容。

    你能用这个扩展你的代码吗:

    if (file_exists($file)) {
     Your code goes here
     ....
     exit();
    }
    

    要检查的其他事项:网络服务器用户是否可以读取文件(如果您使用的是网络服务器)。 该问题对文件名中有特殊字符的文件有影响吗?

    【讨论】:

      【解决方案3】:

      您应该放置一个检查器以查看该文件是否存在于您的文件系统中。

      if (!file_exists($filePath)) {
      
          // Throw an exception or do something for alert the wrong path.
          throw new Exception('File with this path is not available.');
      
      } else {
      
          // Do your amazing stuff here
      
      }
      

      【讨论】:

      • 如果您阅读问题的 cmets,您会看到 OP 已经这样做了。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-09
      • 2018-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多