PHP 读取文件错误

PHP readfile error

我收到一个错误。我正在尝试阅读附件。它在大多数文件上都能完美运行,但在少数文件上我会收到此错误。这些文件具有相同的格式,并且它试图读取的位置是正确的。我已经在 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\asdgasdsf3\Suppliers\saf342\Files\Revit16\Seinätikas.rfa): failed to open stream: No such file or directory in C:\xampp\htdocs\web\downloadattachment.php on line 58

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

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

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

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

作为 Johndodo 类型的引用,您需要确保 PHP 使用正确的内部字符集和编码进行操作,以便它识别文件在您的 (windows ) 目录结构。看什么character set your windows system is using and then use that same character set for PHP internal encoding.

编辑:

逻辑过程为:

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

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

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

}