简体   繁体   English

仅在下载完成并解压缩PHP后才执行代码

[英]Execeute code only after download complete and extract PHP

I have a file to download via curl in PHP about 16Mb, is a zip file,m I want to download and when is finished to download extract it, after extract parse every file inside it. 我有一个文件可以通过PHP在curl中下载,大小约为16Mb,是一个zip文件,我要下载,完成下载后将其解压缩,然后解析其中的每个文件。

This is my code: 这是我的代码:

        $ch2=curl_init();
        curl_setopt($ch2, CURLOPT_URL, $this->URL);
        curl_setopt($ch2, CURLOPT_TIMEOUT, 5040);
        curl_setopt($ch2, CURLOPT_HEADER, 0);
        curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch2, CURLOPT_POST, 1);
        curl_setopt($ch2, CURLOPT_POSTFIELDS,$this->XMLRequest);
        curl_setopt($ch2, CURLOPT_SSL_VERIFYHOST, 1);
        curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, FALSE); 
        curl_setopt($ch2, CURLOPT_SSLVERSION, 3); 
        curl_setopt($ch2, CURLOPT_FOLLOWLOCATION, true);

        $httpHeader2 = array(
            "Content-Type: text/xml; charset=UTF-8",
            "Content-Encoding: UTF-8"
        );

        // Execute request, store response and HTTP response code      

        $xml = curl_exec($ch2);
        $this->errno=curl_getinfo( $ch2, CURLINFO_HTTP_CODE );

        curl_close($ch2);

        $file2 = fopen('json_upload/item.zip','w+');
        fwrite($file2, $xml);
        fclose($file2);

        $zip = new ZipArchive;
        echo'<p>json_upload/item.zip</p>';
        $zip->open('json_upload/item.zip', ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE);
        $zip->extractTo('json_upload/item/');
        $zip->close();

In this mode the file downloaded is 1,6Mb not all and if I try to extract manually return me an error like is damaged. 在这种模式下,下载的文件不是全部​​1,6Mb,如果我尝试手动提取文件,则会返回错误,例如损坏。

If I comment the extract code of the zip the file is downloaded completely and if I extract it manually it works fine. 如果我评论了zip的提取代码,则文件将被完全下载,如果我手动将其提取,则可以正常工作。

How can I execute the extract of the zip only when the download is complete? 仅在下载完成后,如何才能执行zip的提取?
And after how can I execute the parse of each file inside only when the extract command finished? 而且,只有提取命令完成后,如何才能执行内部每个文件的解析?

Thanks 谢谢

Instead of using fwrite , use file_put_contents() 代替使用fwrite ,使用file_put_contents()

Replace 更换

$file2 = fopen('json_upload/item.zip','w+');
fwrite($file2, $xml);
fclose($file2);

With

file_put_contents('json_upload/item.zip', $xml);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM