簡體   English   中英

PHP或Apache? 將顯示zip文件的內容,而不是下載zip文件

[英]Php or Apache? Content of zip file is diplayed instead of downloading zip file

我試圖編寫一個php腳本來創建和下載一個zip文件。 當我在本地主機上測試腳本時,下載就可以了,但是當它上載到服務器時,事情就出了問題:不是下載zip文件,而是在瀏覽器中顯示文件的內容。

有人可以指出我正確的方向嗎?

編碼

   $zip = new ZipArchive();
   $zip->open("$maand/zipfile.zip", ZipArchive::OVERWRITE);
   $zip->addFile("$maand/ant/a_nb.txt", 'ant.txt');
   $zip->addFile("$maand/lim/l_nb.txt", 'lim.txt');
   $zip->addFile("$maand/oos/o_nb.txt", 'oos.txt');
   $zip->addFile("$maand/vla/v_nb.txt", 'vla.txt');
   $zip->addFile("$maand/wes/w_nb.txt", 'wes.txt');
   $zip->close();
   $filename = "zipfile.zip";
   $filepath = "$maand/";
// headers for zip downloads
   header("Pragma: public");
   header("Expires: 0");
   header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
   header("Cache-Control: public");
   header("Content-type: application/zip");
   header("Content-Disposition: attachment; filename=\"".$filename."\"");
   header("Content-Length: ".filesize($filepath.$filename));
   ob_end_flush();
   @readfile($filepath.$filename); 

您缺少ob_start()

例:

    header('Content-Type: application/csv');
    header('Content-Disposition: attachement; filename="' . $download . '"');
    ob_start();
    $str = '';
    if(file_exists($file) === true){
        $str = file_get_contents($file);
    }
    ob_end_clean();
    echo $str;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM