Unzipping larger files with PHP

五迷三道 提交于 2019-12-05 06:39:25
Quamis

Why do you read the whole file at once?

 $buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
 fwrite($fp,"$buf");

Try to read small chunks of it and writing them to a file.

Just because a zip is less than PHP's memory limit & perhaps the unzipped is as well, doesn't take account of PHP's overhead generally and more importantly the memory needed to actually unzip the file, which whilst I'm not expert with compression I'd expect may well be a lot more than the final unzipped size.

For a file of that size, perhaps it is better if you use shell_exec() instead:

shell_exec('unzip archive.zip -d /destination_path');

PHP must not be running in safe mode and you must have access to both shell_exec and unzip for this method to work.

Update:

Given that command line tools are not available, all I can think of is to create a script and send the file to a remote server where command line tools are available, extract the file and download the contents.

function my_unzip($full_pathname){

    $unzipped_content = '';
    $zd = gzopen($full_pathname, "r");

    while ($zip_file = gzread($zd, 10000000)){
        $unzipped_content.= $zip_file;
    }

    gzclose($zd);

    return $unzipped_content;

}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!