PHP强制下载文件已损坏

PHP强制下载文件已损坏,php,download,corrupt,Php,Download,Corrupt,我正在使用此PHP脚本下载一个文件: $status=stripos($_SERVER['HTTP_REFERER'],'servername'); if($status===false) { header('Location:http://xyz.com); } else { header('Content-disposition: attachment; filename=XXX.pdf'); header('Content-type: application/pdf

我正在使用此PHP脚本下载一个文件:

$status=stripos($_SERVER['HTTP_REFERER'],'servername');
if($status===false)
{
    header('Location:http://xyz.com);
}
else
{
    header('Content-disposition: attachment; filename=XXX.pdf');
    header('Content-type: application/pdf');
    readfile('http://www.xyz.com/Downloads/XXX.pdf);
}
但是,当我在任何浏览器中下载该文件时,都会显示该文件已损坏


请帮忙

但这段代码运行良好

header("Content-type: application/pdf");
header('Content-Disposition: attachment; filename="sample.pdf"');
readfile('/var/www/sample.pdf');
请将filename=XXX.pdf更改为filename=XXX.pdf并检查

哦,对不起!!您应该提供内容的基本路径或相对pat,这意味着它应该类似于readfile'/var/www/Downloads/XXX.pdf;相当于读文件'http://www.xyz.com/Downloads/XXX.pdf;


谢谢

我找到了一个我想分享的解决方案, 在我的c代码中[单击下载按钮]-

Response.Redirect("http://myserver.com/download.php?file=DownloadItem/" + fileName + "&type=" + Common.MimeType(fileName) + "&disp=" + fileName + "");    
在我的PHP文件中[


Thanx。但是,我的readfile参数具有整个文件路径,因为它是动态的,我在运行时将文件路径传递给php文件,并将其作为查询字符串传递。headerContent配置:附件;文件名=$\u GET[disp];headerContent类型:$\u GET[type];readfile$\u GET[file];其中$\u GET[disp]=XXX.pdf$\u GET[type]=application/pdf$\u GET[file]=“http://www.xyz.com/Downloads/XXX.pdf”在这种情况下,文件名可能在运行时更改为xyz.jpg,在这种情况下,$\u GET[type]将作为image/jpeg传递。请注释.Thanx以获取信息。但是,我的readfile参数具有整个文件路径,因为它是动态的,我在运行时将文件路径传递给php文件,并将其作为查询字符串传递。headerContent配置:附件;文件名=$\u GET[disp];headerContent类型:$\u GET[type];readfile$\u GET[file];其中$_-GET[disp]=XXX.pdf$_-GET[type]=application/pdf$_-GET[file]='http://www.xyz.com/Downloads/XXX.pdf'在这种情况下,文件名可能在运行时更改为xyz.jpg,在这种情况下$_-GET[type]将作为image/jpeg传递。
$status=stripos($_SERVER['HTTP_REFERER'],'myserver.com');<br>
if($status===false)
{
header('Location:http://myserver.com/ErrorPage');<br>
}
else
{
header("Content-disposition:attachment;filename= $_GET[disp]");
header("Content-type: $_GET[type]");
readfile("$_GET[file]");
}