Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/270.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>;下载ZIP:网络错误_Php - Fatal编程技术网

PHP>;下载ZIP:网络错误

PHP>;下载ZIP:网络错误,php,Php,我的php代码有一个很大的问题,我使用html表单获取“文件名”,工作得很好,但我的问题是:当我启动下载时,所有浏览器的下载zip文件,并获取网络错误,例如:578ko/600ko:网络错误 <?php $dir = "lol/"; // trailing slash is important $file = $dir .$_POST['filename'] ; if (file_exists($file)) { header('Pragma: public'); hea

我的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;
?>

您可以尝试读取和发送块-这可能会有所帮助

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

        /*
            send the file in chunks rather than trying to read and send all at once
        */
        if( $fh = @fopen( $file, 'rb' ) ) {
            while( !@feof( $fh ) and ( connection_status()==0 ) ) {
                print( fread( $fh, 1024*8 ) );
                flush();
            }
            @fclose( $fh );
        }

    } else {
        echo "Le fichier $file n'existe pas.";
    }
    exit;
?>

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

<?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;
?>

尝试增加时间限制您的Apache超时、KeepAlive和KeepAliveTimeout值是多少?如果没有设置指令,默认值可能会让您感到不适。我只是扩展了值,什么都没有:/@Teddy76db删除所有标题,只添加这些标题,然后尝试让我知道。标题(“内容类型:应用程序/zip”);标题('Content-Disposition:'attachment;filename=“”.$basename($file)。“”)标题('Content-Length:'.filesize($file));SmileIT解决了我的问题,也谢谢dude的测试,它在我系统上的文件硬编码路径下运行良好。该解决方案也解决了我在下载.zip文件后出现的“无效存档”问题。输出缓冲区可能会更改响应的某些标题或信息,从而使归档结构混乱。Rar文件仍然可以工作,但警告有“奇怪”的头。