Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php还是Apache?将显示zip文件的内容,而不是下载zip文件_Php_Apache_Download - Fatal编程技术网

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

Php还是Apache?将显示zip文件的内容,而不是下载zip文件,php,apache,download,Php,Apache,Download,我试图编写一个php脚本来创建和下载一个zip文件。当我在本地主机上测试脚本时,下载工作正常,但是当它被上传到服务器时,事情就出了问题:不是下载zip文件,而是在浏览器中显示文件的内容 谁能给我指一下正确的方向吗 代码 $zip = new ZipArchive(); $zip->open("$maand/zipfile.zip", ZipArchive::OVERWRITE); $zip->addFile("$maand/ant/a_nb.txt", 'ant.t

我试图编写一个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\u 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;

看看这里:您必须设置正确的标题。您不需要
ob_end_flush()