I'm trying to write a PHP script to download a zip file from a web server that contains a single CSV file and then load the contents of the extracted CSV file into an existing MySQL database table.
$targetFile = 'data-' . md5(microtime()) . '.zip';
$url = 'http://www.address.com/data.zip';
$out = fopen('/path/to/zip/save/folder/' . $targetFile , 'wb');
$ch = curl_init();
curl_setopt($ch, CURLOPT_FILE, $out);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_exec($ch);
$info = curl_getinfo($ch);
if($info['http_code'] == 200 && $info['content_type'] == 'application/x-zip-compressed') {
$zh = zip_open('/path/to/zip/save/folder/' . $targetFile);
}
else {
exit('Download of ZIP file failed.');
}
The above code does manage to download the file to a directory on the server with a unique name. But I am unable to extract the contents.
I've tried using PHP's zip_open
command to extract the zip but it always returns an error code of 19
instead of a handle. I've checked the path that I pass to the zip_open function and it's a full system path i.e. /path/to/zip/save/folder/data-5384e2306718492958f20e68de95c6fa.zip
.
Note:
The CSV file file is 2.5 MB compressed and 30 MB uncompressed.