2012-07-17 78 views
1

这个问题是微不足道的,有很多答案,都是一样的或差不多,但对我的情况,它没有按预期解决? 目标:发送WORD文件作为附件与PHP(简单...) 平均:这里是代码:PHP:发送WORD文档文件下载

// send the file to the browser 
header("Cache-Control: no-store"); 
header("Content-Type: application/octet-stream"); 
//header("Content-type: application/msword"); 
header('Content-Disposition: attachment; filename="'. basename($filename) . '"'); 
header('Content-Transfer-Encoding: binary'); 
header('Content-Length: '. filesize($filename)); 
ob_clean(); 
flush(); 
readfile($filename); 
exit(); 

好了,一切似乎是正确的,但我救了我的计算机上的文件不能被MS-阅读WORD:它读取一些特殊字符如:

PK! /Œt1«[Content_Types] .xml ...

但是,如果我从服务器打开原件,一切都OK。 我错过了一些明显的... 任何建议是值得欢迎的,因为我尝试了几乎所有我读过的方法......但仍然是相同的结果。

+0

可能不相关,但仍值得去做:删除未定义的头部字段,如Content-Transfer-Encoding和不需要的头部字段(如Cache-Control)。 – 2012-07-17 11:44:19

回答

2

也许你的uotput文件得到错误的标题。我在我的脚本中有这样的内容,我用它来下载文件:

$tmp = explode(".",$file['filename']); 
switch ($tmp[count($tmp)-1]) { 
    case "pdf": $ctype="application/pdf"; break; 
    case "exe": $ctype="application/octet-stream"; break; 
    case "zip": $ctype="application/zip"; break; 
    case "docx": 
    case "doc": $ctype="application/msword"; break; 
    case "csv": 
    case "xls": 
    case "xlsx": $ctype="application/vnd.ms-excel"; break; 
    case "ppt": $ctype="application/vnd.ms-powerpoint"; break; 
    case "gif": $ctype="image/gif"; break; 
    case "png": $ctype="image/png"; break; 
    case "jpeg": 
    case "jpg": $ctype="image/jpg"; break; 
    case "tif": 
    case "tiff": $ctype="image/tiff"; break; 
    case "psd": $ctype="image/psd"; break; 
    case "bmp": $ctype="image/bmp"; break; 
    case "ico": $ctype="image/vnd.microsoft.icon"; break; 
    default: $ctype="application/force-download"; 
} 

header("Pragma: public"); // required 
header("Expires: 0"); 
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
header("Cache-Control: private",false); // required for certain browsers 
header("Content-Type: $ctype"); 
header("Content-Disposition: attachment; filename=\"".$filename."\";"); 
header("Content-Transfer-Encoding: binary"); 
header("Content-Length: ".$fsize); 
ob_clean(); 
flush(); 
readfile('files/'.$file['filename']); 

我从db获取的文件名。

+0

mime类型为.docx不是application/msword,它是application/vnd.openxmlformats-officedocument.wordprocessingml.document同样,你对xlsx也是错误的 – 2012-07-17 08:32:34

+0

thx,我可以在哪里找到相关文档?我前一段时间从PHP手册中拿过来。 – miszczu 2012-07-17 08:34:18

+0

所有OfficeOpenXML文件类型的MIME类型http://technet.microsoft.com/en-us/library/ee309278.aspx或所有Office(BIFF和OfficeOpenXML)文件类型http://blogs.msdn.com/b/vsofficedeveloper/ archive/2008/05/08/office-2007-open-xml-mime-types.aspx – 2012-07-17 08:36:59

2

从你展示,你创建一个OfficeOpenXML(.DOCX)文件,而不是一个BIFF(.DOC)文件,所以内容类型应该是

application/vnd.openxmlformats-officedocument.wordprocessingml.document 

和内容的几个字符文件扩展名应该是.DOCX

+0

听起来合乎逻辑但同样的问题。实际上,现在显示的字符有点不同:“PK <!@ @ \t docProps/PK!-''docProps /app.xmlRÁNã0½ÿÿh h 012 H pD-3I, e»ù{&ÚÁ“。看来我可以阅读更多,但总是有同样的问题。尽管如此,在服务器上,文件还是可以的。 – 2012-07-18 07:40:17

-1

呼叫https://stackoverflow.com/download-script.php?file=example.doc

这是下载的script.php代码

if (isset($_GET['file'])) {  
    $file = $_GET['file']; 

    if (file_exists($file) && is_readable($file) && preg_match('/\.doc$/',$file)) { 
    header('Content-Type: application/doc'); 
    header("Content-Disposition: attachment; filename=\"$file\""); 

    readfile($file); 
    }  
} else { 
    header("HTTP/1.0 404 Not Found"); 

    echo "<h1>Error 404: File Not Found: <br /><em>$file</em></h1>"; 
}