PHP下载脚本会破坏文件

时间:2011-11-20 21:30:39

标签: php download

可能是什么问题,服务器上的文件是好的,但是在我按下下载之后就会损坏......

<?php
if (isset($_POST['submit'])) {
    header('Content-type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
    header('Content-Disposition: attachment; filename="ataskaita.docx"');
    readfile('../generavimui/ataskaita.docx'); 
}
?>

3 个答案:

答案 0 :(得分:2)

使用记事本或十六进制编辑器查看文件。那里可能有一条PHP错误消息。

可能的原因包括

  • 您要查找的文件不存在
  • $_POST['submit']未设置

答案 1 :(得分:1)

在读取文件

之前尝试重新设置输出缓冲区
if (isset($_POST['submit'])) {
    header('Content-type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
    header('Content-Disposition: attachment; filename="ataskaita.docx"');
    ob_clean();
    flush();
    readfile('../generavimui/ataskaita.docx'); 
}

答案 2 :(得分:0)

我遇到了同样的问题,除了我还有一些其他问题,比如下载的文件中包含拖尾源代码。

要解决此问题,请将此代码替换为:

header('Content-Description: File Transfer'); 
header('Content-type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header('Content-Disposition: attachment; filename="ataskaita.docx"'); 
header('Content-Transfer-Encoding: binary'); 
header('Expires: 0'); 
header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
header('Pragma: public'); 
header('Content-Length: ' . filesize($file)); 

Source Site

希望这有帮助