Php 强制下载文件断头错误?

Php 强制下载文件断头错误?,php,header,save,download,Php,Header,Save,Download,我一下载并打开文件,就会收到一条错误消息。当我试图打开一个.doc文件时,我得到一条消息:文件结构无效。 当我试图打开一个.jpg文件时:这个文件无法打开。它可能已损坏,或者是预览无法识别的文件格式 但当我下载PDF文件时,它们会毫无问题地打开 有人能帮我吗 另外,我尝试了不同的标题,包括: 标题(“内容类型:应用程序/八位字节流”) 标题与实际内容无关,因此问题在于实际内容有问题 此外,您确实不应该试图通过更改内容类型头来强制下载。让它保持原样。让客户端决定如何处理它。确保只输出readfil

我一下载并打开文件,就会收到一条错误消息。当我试图打开一个.doc文件时,我得到一条消息:文件结构无效。 当我试图打开一个.jpg文件时:这个文件无法打开。它可能已损坏,或者是预览无法识别的文件格式

但当我下载PDF文件时,它们会毫无问题地打开

有人能帮我吗

另外,我尝试了不同的标题,包括:
标题(“内容类型:应用程序/八位字节流”)

标题与实际内容无关,因此问题在于实际内容有问题


此外,您确实不应该试图通过更改内容类型头来强制下载。让它保持原样。让客户端决定如何处理它。

确保只输出
readfile()
数据:确保PHP代码以文件末尾的
开头,如空格或换行符(或完全删除
?>

$fsize=filesize($yourFilePath); $allowed\u ext=数组( //档案 “zip”=>“应用程序/zip”, //文件 'pdf'=>'应用程序/pdf', 'doc'=>'应用程序/msword', “xls”=>“应用程序/vnd.ms excel”, 'ppt'=>'应用程序/vnd.ms powerpoint', ); $mtype=mime\u content\u type($file\u path); 如果($mtype==''){ $mtype=“应用程序/强制下载”; } 标题(“Pragma:public”); 标题(“到期日:0”); 标头(“缓存控制:必须重新验证,后检查=0,前检查=0”); 标头(“缓存控制:公共”); 标题(“内容描述:文件传输”); 标题(“内容类型:$mtype”); 标题(“内容处置:附件;文件名=\”$asfname\”); 标题(“内容传输编码:二进制”); 标题(“内容长度:.$fsize”); //那你的休息代码呢
在记事本中打开PHP文件。按“另存为”并选择“ASCI”文件格式。这对我的情况很有帮助,因为文件是UTF8格式的,这就是原因

if($_POST['mode']=="save") {
$root = $_SERVER['DOCUMENT_ROOT'];
$path = "/mcboeking/";
$path = $root.$path;
$file_path = $_POST['path'];
$file = $path.$file_path;

if(!file_exists($file)) {
    die('file not found');
} else {
    header("Cache-Control: public");
    header("Content-Description: File Transfer");
    header('Content-Type: application/force-download');
    header("Content-Disposition: attachment; filename=\"".basename($file)."\";" );
    header("Content-Length: ".filesize($file));
    readfile($file);}}
$fsize = filesize($yourFilePath); $allowed_ext = array ( // archives 'zip' => 'application/zip', // documents 'pdf' => 'application/pdf', 'doc' => 'application/msword', 'xls' => 'application/vnd.ms-excel', 'ppt' => 'application/vnd.ms-powerpoint', ); $mtype = mime_content_type($file_path); if ($mtype == '') { $mtype = "application/force-download"; } header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Cache-Control: public"); header("Content-Description: File Transfer"); header("Content-Type: $mtype"); header("Content-Disposition: attachment; filename=\"$asfname\""); header("Content-Transfer-Encoding: binary"); header("Content-Length: " . $fsize); //then ur rest code