使用PHP强制下载文件:损坏的文件,尝试了几种解决方案

Iva*_*van 0 php file download

这已经发布了,但我已经尝试了很多在SO上找到的解决方案(例如:http://davidwalsh.name/php-force-download)

我基本上有这个:

{       
    $filePath = '../public/myfile.png';
    $fileName = basename($filePath);
    $fileSize = filesize($filePath);


    if (!is_file) {
        die("File not found");
    } else {                        
        header("Content-Description: File Transfer");
        header('Content-Type: application/octet-stream');
        header("Content-Disposition: attachment; filename= " . $fileName);
        header("Content-Transfer-Encoding: binary");
        readfile($filePath);
    }
}
Run Code Online (Sandbox Code Playgroud)

文件被识别和下载,但.PNG是空的.DOC已损坏(Word要求我修复文件,然后没关系).我也试过PDF,没问题.

我打算放置所有选项(Pragma,Cache-Control,Expires,Content-Length等),仍然下载的文件但在某种程度上已损坏...

你有没有遇到过我的问题?请考虑我在IIS 7.5上

提前致谢

pow*_*tac 8

使用Notepad ++等纯文本编辑器打开下载的文件.在顶部你会发现一个PHP错误通知,它会告诉你出了什么问题.

错误可能是"会话已发送".然后ob_start();在脚本的开头添加.

  • 您可能还想使用此技巧来查看文件开头是否有任何空格.它将阻止图像正确加载到除photoshop之外的所有内容. (2认同)