Php 下载脚本非常慢

Php 下载脚本非常慢,php,javascript,download,Php,Javascript,Download,我已经用javascript和php编写了一个下载脚本。它可以工作,但是如果我想下载一个大文件(例如1GB zip文件),那么它需要很长时间才能完成请求。我想这和我读文件有关。如果是这样的话,你知道如何加快下载速度吗?注意:我需要一个标题,这是强制下载图像、PDF或任何类型文件的原因 JS非常简单。看看这个: function downloadFile(file){ document.location.href = "script.php?a=downloadFile&b=."+

我已经用javascript和php编写了一个下载脚本。它可以工作,但是如果我想下载一个大文件(例如1GB zip文件),那么它需要很长时间才能完成请求。我想这和我读文件有关。如果是这样的话,你知道如何加快下载速度吗?
注意:我需要一个标题,这是强制下载图像、PDF或任何类型文件的原因

JS非常简单。看看这个:

function downloadFile(file){
    document.location.href = "script.php?a=downloadFile&b=."+ file;
}
PHP非常简单:

function downloadFile($sFile){
    #Main function
    header('Content-Type: '.mime_content_type($sFile)); 
    header('Content-Description: File Transfer');
    header('Content-Length: ' . filesize($sFile)); 
    header('Content-Disposition: attachment; filename="' . basename($sFile) . '"');
    readfile($sFile);
}

switch($_GET['a']){

    case 'downloadFile':
        echo downloadFile($_GET['b']);
        break;
}

我想缓冲是大文件的问题。 尝试以小块(如兆字节)读取文件,并在打印每个块后调用函数刷新输出缓冲区

编辑:嗯,好的,下面是您应该尝试的代码示例:

function downloadFile($sFile){
    #Main function

    if ( $handle = fopen( $sFile, "rb" ) ) {
        header('Content-Type: '.mime_content_type($sFile)); 
        header('Content-Description: File Transfer');
        header('Content-Length: ' . filesize($sFile)); 
        header('Content-Disposition: attachment; filename="' . basename($sFile) . '"');

        while ( !feof($handle) ) {
            print fread($handle, 1048576);
            flush();
        }
        fclose($handle);
    } else {
        header('Status: 404');
        header('Content-Type: text/plain');
        print "Can't find the requested file";
    }
}

我想缓冲是大文件的问题。 尝试以小块(如兆字节)读取文件,并在打印每个块后调用函数刷新输出缓冲区

编辑:嗯,好的,下面是您应该尝试的代码示例:

function downloadFile($sFile){
    #Main function

    if ( $handle = fopen( $sFile, "rb" ) ) {
        header('Content-Type: '.mime_content_type($sFile)); 
        header('Content-Description: File Transfer');
        header('Content-Length: ' . filesize($sFile)); 
        header('Content-Disposition: attachment; filename="' . basename($sFile) . '"');

        while ( !feof($handle) ) {
            print fread($handle, 1048576);
            flush();
        }
        fclose($handle);
    } else {
        header('Status: 404');
        header('Content-Type: text/plain');
        print "Can't find the requested file";
    }
}

您可以从切换到downloadFile时删除回显,而不是一次读取整个文件,而是通过一次读取并回显一点文件来将其分块。您可以从切换到downloadFile时删除回显,而不是一次读取整个文件,通过一次读取并回显一点文件来将其分块。我真的很想使用从浏览器下载的内容。如果我打开www.example.com/some.zip,浏览器会自动启动下载文件。是否没有机会使用任何扩展文件(图片、PDF等)强制下载,或者我必须用区块文件发送一个头文件?嗯,什么?。。我已经用一个你应该尝试的例子更新了答案。我真的很想使用从浏览器下载的内容。如果我打开www.example.com/some.zip,浏览器会自动启动下载文件。是否没有机会使用任何扩展文件(图片、PDF等)强制下载,或者我必须用区块文件发送一个头文件?嗯,什么?。。我已经用一个你应该尝试的例子更新了答案。