简体   繁体   English

PHP或Apache? 将显示zip文件的内容,而不是下载zip文件

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

I tried to write a php-script to create and download a zip file. 我试图编写一个php脚本来创建和下载一个zip文件。 When I tested the script on my localhost, the download works, but when it's uploaded to the server, things go wrong: instead of downloading the zip file, the content of the file is displayed in the browser. 当我在本地主机上测试脚本时,下载就可以了,但是当它上载到服务器时,事情就出了问题:不是下载zip文件,而是在浏览器中显示文件的内容。

Can somebody point me in the right direction? 有人可以指出我正确的方向吗?

The code 编码

   $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); 

you missing ob_start() 您缺少ob_start()

example: 例:

    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