【问题标题】:How to update legacy PHP code that is corrupting docx and xlsx files?如何更新损坏 docx 和 xlsx 文件的旧 PHP 代码?
【发布时间】:2013-04-19 00:04:23
【问题描述】:

我正在使用一些遗留的 PHP 代码并且遇到了 Word Doc (docx) 和电子表格 (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); 

非常感谢任何帮助。谢谢

【问题讨论】:

    标签: php header docx corruption xlsx


    【解决方案1】:

    道具How to download word file using php on chrome and IE

    我用它来修复 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();
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-07
      • 2012-12-05
      • 2020-01-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多