强制文件下载与PHP:损坏的文件,已经尝试了几个解决方案 [英] Force file download with PHP: corrupted files, have tried several solutions

查看:143
本文介绍了强制文件下载与PHP:损坏的文件,已经尝试了几个解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已发布,但我已经尝试了许多在SO和更多的解决方案(如下所示: http://davidwalsh.name/php-force-download

This has been posted, but I've tried lot of solutions found on SO and more (like this: 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是空的和.DOCs已损坏(Word要求我修复文件,那就没关系)。我也尝试过PDF,而且没有问题。

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). I have tried also PDFs, and no problem with that.

我尝试将所有选项(Pragma,Cache-Control,Expires,Content-Length等) ),仍然下载的文件,但以某种方式损坏...

I trues to put all sort of options (Pragma, Cache-Control, Expires, Content-Length, etc.), still downloaded files but corrupted in some way...

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

Did you ever had my problem? Please consider I'm on IIS 7.5

提前感谢

推荐答案

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

Open the downloaded files with a plain text editor like Notepad++. At the top you will find a PHP Error notice, it will tell you what's going wrong.

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

The error is probably "session already send". Then add ob_start(); at the beginning of your script.

这篇关于强制文件下载与PHP:损坏的文件,已经尝试了几个解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆