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


Force file download with PHP: corrupted files, have tried several solutions

这已经发布,但我尝试了很多在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);
    }
}

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

我确实放置了各种选项(Pragma,缓存控制,过期,内容长度等),仍然下载文件,但以某种方式损坏了......

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

提前致谢

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

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