How can I download and extract a zipped CSV and get the contents of CSV file into a MySQL table
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/' . $targ开发者_C百科etFile);
}
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.
Remember to strip response headers before saving content as file. Also, you can check if there was HTTP/1.1 200 OK response, or 301/302 redirects to follow
zip_open() error number 19 is: "Zip File Function error: Not a zip archive". That means your script did not download the zip file properly.
$url="http://www.some.zip"; $target = 'data-' . md5(microtime()) . '.zip';
function download($src, $dst) {
$f = fopen($src, 'rb');
$o = fopen($dst, 'wb');
while (!feof($f)) {
if (fwrite($o, fread($f, 2048)) === FALSE) {
return 1;
}
}
fclose($f);
fclose($o);
return 0;
}
download($url,$target);
if ( file_exists($target) ){
# do your unzipping..
}
I use this
$r = new HTTPRequest($url);
$r->setOptions($options);
$r->send();
$code = $r->getResponseCode();
if (200 != $code) {
throw ...;
}
file_put_contents($zip, $r->getResponseBody());
$arc = new ZipArchive;
if (true !== $arc->open($zip)) {
throw ...;
}
file_put_contents($out, $arc->getFromIndex(0));
you should be able to pick it up from there
It seems that the problem was due to a missing fclose
call before I tried to open the ZIP file.
精彩评论