【问题标题】:Php or Apache? Content of zip file is diplayed instead of downloading zip filePHP 还是 Apache?显示 zip 文件的内容,而不是下载 zip 文件
【发布时间】:2016-03-17 01:03:26
【问题描述】:

我尝试编写一个 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); 

【问题讨论】:

标签: php apache download


【解决方案1】:

你错过了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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-27
    相关资源
    最近更新 更多