dongyixun0634 2013-07-10 07:16
浏览 63

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

I have a script that allow user to download file with filesize > 250MB. When the filesize is < 100MB, it's downloadable. But not with file > 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 

But still it's not workable. How to make it so that i can download file with > 250MB ?

Update: code to download the zip file

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条回答 默认 最新

  • doucheng9634 2013-07-11 01:54
    关注

    Ok..so now i can get my file to be downloaded. But, once i want to extract the zip file, error msg is given, saying that: The archive is either in unknown format or damaged. I check the copy of the zip file in the original folder n try to extract, it works fine. No problem in extracting the data. Below is the script:

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

    the File size that I retrieve is also correct. What is the problem?

    评论

报告相同问题?

悬赏问题

  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂