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.
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.
If I comment the extract code of the zip the file is downloaded completely and if I extract it manually it works fine.
How can I execute the extract of the zip only when the download is complete?
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()
Replace
$file2 = fopen('json_upload/item.zip','w+');
fwrite($file2, $xml);
fclose($file2);
With
file_put_contents('json_upload/item.zip', $xml);