PHP - 异步提取大型ZIP文件

时间:2014-05-25 15:47:20

标签: php codeigniter asynchronous zip extraction

过去几天我一直在努力解决这个问题,所以也许有人可以帮我解决这个问题。我正在开发一个大型应用程序,它必须提取一个包含大约3-4GB CSV文件的~200MB ZIP文件。

为此,我编写了一个jQuery插件,该插件应该在ZIP解压缩时向用户显示内容,并处理超时。但是一旦提取过程开始,Ajax就无法发送“它已经完成了吗?”请求到服务器,因为它们也超时。所以我的问题是 - 有没有办法异步启动ZIP提取然后检查进度?

我正在CodeIgniter框架上构建这个应用程序,并且zip提取功能如下所示:

public function unpack_zip ($zip_file) {
    // First we check the database to see if the extraction process has already started
    $extracting_file = $this->db->select('value')->from('setting')->where(array('type' => 0, 'key' => 'tube_extractng'))->get()->result_array();

    if (!!$extracting_file[0]['value']) return 'Already extracting!';

    // Next we create a strimg containing path to the zip file.
    $source_file = $this->install_assets_dir . 'zip' . DIRECTORY_SEPARATOR . $zip_file;

    $zip = new ZipArchive;

    if ($zip->open($source_file) === TRUE) {
            // If the zip was open sucessfully we tell the database that extraction is in progress, this stays "true" until the process completes
        $this->db->update('setting', array('value' => true), array('type' => 0, 'key' => 'tube_extractng'));

        set_time_limit(600); // The timeout long enough to extract an extra large file
        $zip->extractTo($this->install_assets_dir . 'csv' . DIRECTORY_SEPARATOR);
        $zip->close();

        rename($source_file, preg_replace('/\.zip$/', '.extracted$0', $source_file)); // Renaming the extracted zip by adding ".extracted" before the extension
        $this->db->update('setting', array('value' => false), array('type' => 0, 'key' => 'tube_extractng')); // And, of course tell the database that the process is done.
        return TRUE;
    }

    return FALSE;

}

问题是 - 一旦进程启动,服务器就会无响应,直到提取完成。无响应的服务器=没有回答“你还在提取吗?” Ajax调用。

1 个答案:

答案 0 :(得分:0)

在深入研究之后,我发现了问题所在。我正在使用WT-NMP,我的PHP被设置为1个PHP-CGI服务器,实际上当一个正在进行时阻止所有其他请求。将该数字设置为2已经开始回复“你完成了吗?” ajax请求。这件事现在可以想象了。

因此对于有类似问题的其他人 - 首先检查您正在运行的PHP CGI服务器的数量。 :)