PHP > 下载 ZIP:网络错误

Ted*_*6db 2 php

我的 php 代码有一个大问题,我使用 html 表单来获取“文件名”,工作完美,但我的问题是:当我启动下载时,所有浏览器都下载 zip 文件,并出现网络错误,例如:578ko / 600ko:网络错误。

<?php
$dir = "lol/"; // trailing slash is important
$file = $dir .$_POST['filename'] ;

if (file_exists($file)) {
    header('Pragma: public');
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: public");
    header("Content-Transfer-Encoding: binary");
    header("Content-type: application/zip");
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Cache-Control: must-revalidate');
    header('Content-Length: ' . filesize($file));

    readfile($file);

} else {
    echo "Le fichier $file n'existe pas.";
}
exit;
?>
Run Code Online (Sandbox Code Playgroud)

Smi*_*eIT 5

检查您的网络服务器超时值并增加/定义为更高的值。同时关闭输出缓冲。

<?php
$dir = "lol/"; // trailing slash is important
$file = $dir .$_POST['filename'] ;
//Turn off output buffering
if (ob_get_level())
   ob_end_clean();

if (file_exists($file)) {
    header('Pragma: public');
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: public");
    header("Content-Transfer-Encoding: binary");
    header("Content-type: application/zip");
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Cache-Control: must-revalidate');
    header('Content-Length: ' . filesize($file));

    readfile($file);

} else {
    echo "Le fichier $file n'existe pas.";
}
exit;
?>
Run Code Online (Sandbox Code Playgroud)