PHP 强制下载的另一个问题。 它正在工作,然后我做了一些事情,现在它没有了。
我们来了(我接受的每种文件类型都有 if 语句,但仅显示一个应该足以帮助诊断问题):
$string1 = 'home/myhost/aboveroot/reviews/';
$string2 = $username;
$string3 = '/';
$string4 = $filename;
$file= $string1.$string2.$string3.$string4;
$ext = strtolower (end(explode('.', $filename)));
//Finding MIME type
if ($ext = pdf){
header("Content-disposition: attachment; filename= '$filename'");
header('Content-type: application/pdf');
readfile('$file');
最终发生的事情是它强制下载具有正确文件名和正确扩展名的文件。 它不会触发任何类型的损坏文件错误。 但是,当我打开文件时,内容丢失了。 如果是word文档,它将拒绝打开。 如果它是一个文本文档,文本将被更改为一堆php错误,如下所示:
警告:readfile($file) [function.readfile]:无法打开流:在/home/myhost/public_html/wwww.mydomain.com/pagethatforceddownload.php 第 68
行中没有这样的文件或目录
然后,它为带有标头的每一行(即使是包含在不应满足其条件的 if 子句中的那些)给出此错误:
警告:无法修改标头信息 - 标头已由/home/myhost/public_html/wwww.mydomain.com/pagethatforceddownload.php:68 发送(输出始于/home/myhost/public_html/wwww.mydomain.com/pagethatforceddownload.php 第 69
行
我真的迷茫了。任何帮助将不胜感激。 我终于让我网站上的所有东西都正常工作和 BAM,我意识到我遇到了这么大的问题。
这一行是错误的:
if ($ext = pdf){
它应该是:
if ($ext == "pdf"){
您可以添加:
if($ext == "pdf" && file_exists($file)) {
以确保您获得有效的文件。
这也是错误的:
readfile('$file');
它应该是:
readfile($file);