0

我正在使用一些遗留的 PHP 代码,并且遇到了 Word Doc (docx) 和 Spreadsheet (xlsx) 损坏。

这是 download.php 文件中的当前代码:

$new_file_name = stripMySlashes($filename); 
    header("Content-disposition: attachment;filename=$new_file_name");
    header("Content-type: application/octetstream");
    header("Pragma: no-cache");
    header("Expires: 0");
    $client=getenv("HTTP_USER_AGENT");
    $fp=fopen('uploaded_files/'.$folder.'/'.$filename,"r");
    $str=fread($fp,filesize('uploaded_files/'.$folder.'/'.$filename));
    echo $str;
    fclose($fp);

例如,如何避免在 case 语句中检查一堆文件类型?我试过这样的代码没有运气

$file="test.docx"; 
header("Pragma: public"); 
header('Content-disposition: attachment; filename='.$file); 
header("Content-type: ".mime_content_type($file)); 
header('Content-Transfer-Encoding: binary'); 
ob_clean(); 
flush(); 
readfile($file); 

非常感谢任何帮助。谢谢

4

1 回答 1

0

关于如何在 chrome 和 IE 上使用 php 下载 word 文件的道具

我用它来修复 docx 损坏问题...

$new_file_name = stripMySlashes($filename); 

$fdl = @fopen('uploaded_files/'.$folder.'/'.$filename,'rb');
header("Status: 200");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
header("Pragma: hack");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private", false);
header("Content-Description: File Transfer");
header("Content-Type: application/force-download");
header("Content-Type: application/download");
header("Content-Type: application/octetstream");
header("Content-Disposition: attachment; filename=\"".$new_file_name."\""); 
header("Content-Transfer-Encoding: binary");
header("Content-Length:".filesize('uploaded_files/'.$folder.'/'.$filename));  
if($fdl)
{
    while(!feof($fdl)) {
        print(fread($fdl, filesize('uploaded_files/'.$folder.'/'.$filename)));
        flush();
        if (connection_status()!=0) 
        {
            @fclose($fdl);
            die();
        }
    }
}
于 2013-04-19T00:15:12.093 回答