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;