PHP -异步提取大型ZIP文件


PHP - Extract large ZIP file asynchronously

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

为此,我编写了一个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调用。

深入研究后,我发现了问题所在。我使用的是WT-NMP,我的PHP设置为1个PHP- cgi服务器,当一个请求正在进行时,它实际上会阻止所有其他请求。将该数字设置为2已经开始对"您完成了吗?"ajax请求提供响应。这东西现在像想象的那样工作了。

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