如何在PHP下载大文件(> 250 MB)?

时间:2013-07-10 07:16:57

标签: php

我有一个脚本,允许用户下载带文件大小的文件> 250MB。当文件大小为< 100MB,可下载。但不是文件> 250 MB。

I have changed the setting in php.ini:
memory_limit = 12800M
post_max_size = 8000M
upload_max_filesize = 2000M
max_execution_time = 512000 

但它仍然不可行。如何制作它以便我可以用>下载文件250MB?

更新:下载zip文件的代码

ini_set('max_execution_time', 512000);

$file_folder = "image/data/";   // folder to load files

$zip = new ZipArchive();            // Load zip library 
$zip_name = "image.zip";            // Zip name
if($zip->open($zip_name, ZIPARCHIVE::CREATE)!==TRUE){       // Opening zip file to load files
    echo  "* Sorry ZIP creation failed at this time<br/>";
}

$dir = opendir ("image/data");
$counter = 0;
while (false !== ($file = readdir($dir))) 
{
    if($file == '.' || $file == '..')
    {   }else
    {
        $zip->addFile($file_folder.$file, $file);
    }
}

$zip->close();

// push to download the zip
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="'.$zip_name.'"');
header('Content-Length: ' . filesize($zip_name));
readfile($zip_name);
// remove zip file is exists in temp path
unlink($zip_name);

1 个答案:

答案 0 :(得分:0)

好的。现在我可以下载我的文件了。但是,一旦我想提取zip文件,就会给出错误信息,说:存档格式未知或已损坏。我检查原始文件夹中的zip文件的副本n尝试提取,它工作正常。提取数据没问题。以下是剧本:

set_time_limit(0);
$file_folder = "image/data/";

$zip = new ZipArchive();    // Load zip library 
$zip_name = "image.zip";             // Zip name
if($zip->open($zip_name, ZIPARCHIVE::CREATE)!==TRUE){       // Opening zip file to load files
    echo  "* Sorry ZIP creation failed at this time<br/>";
}

$dir = opendir ("image/data");
$counter = 0;
while (false !== ($file = readdir($dir))) 
{
    if($file == '.' || $file == '..')
    {   }else
    {
        $zip->addFile($file_folder.$file, $file);
    }
}

$zip->close();
if(file_exists($zip_name)){

    // set headers push to download the zip
    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-Transfer-Encoding: Binary");
    header('Content-Disposition: attachment; filename="'.$zip_name.'"');
    header("Content-Length: ".filesize($zip_name)); 

    //readfile($zip_name);
    // remove zip file is exists in temp path
    //unlink($zip_name);

    $fp = @fopen($zip_name, "rb");
    if ($fp) {
     while(!feof($fp)) {
         echo fread($fp, 1024);
         flush(); // this is essential for large downloads
         if (connection_status()!=0) {
             @fclose($zip_name);
             die();
         }
     }
     @fclose($zip_name);
    }
    unlink($zip_name);
}

我检索的文件大小也是正确的。有什么问题?