简体   繁体   English

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

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

This has been posted, but I've tried lot of solutions found on SO and more (like this: http://davidwalsh.name/php-force-download ) 这已经发布了,但是我已经尝试了许多在SO等上找到的解决方案(例如: http : //davidwalsh.name/php-force-download

I basically have this: 我基本上有这个:

{       
    $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);
    }
}

Files are recognized and downloaded, but .PNGs are empty and .DOCs are corrupted (and Word asks me to fix the file, then it's ok). 可以识别并下载文件,但是.PNG为空,.DOC已损坏(Word要求我修复文件,这样就可以了)。 I have tried also PDFs, and no problem with that. 我也尝试过PDF,这没有问题。

I trues to put all sort of options (Pragma, Cache-Control, Expires, Content-Length, etc.), still downloaded files but corrupted in some way... 我确实要放置各种选项(编译指示,缓存控制,过期,内容长度等),仍然下载文件但以某种方式损坏了...

Did you ever had my problem? 你有我的问题吗? Please consider I'm on IIS 7.5 请考虑我在IIS 7.5上

Thanks in advance 提前致谢

Open the downloaded files with a plain text editor like Notepad++ . 使用纯文本编辑器(如Notepad ++)打开下载的文件。 At the top you will find a PHP Error notice, it will tell you what's going wrong. 在顶部,您会发现一个PHP错误通知,它将告诉您出了什么问题。

The error is probably "session already send". 错误可能是“会话已发送”。 Then add ob_start(); 然后添加ob_start(); at the beginning of your script. 在脚本的开头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM