views:

199

answers:

5

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.

+1  A: 

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.

John Conde
I've updated my example to include the zip_open code. It still returns 19. But the file does appear to have downloaded successful as I can see the file in the folder and extract it's contents using Windows.
Camsoft
+1  A: 

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

dev-null-dweller
A: 

hi, you can try this

$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..
}
ghostdog74
I'm worried about using this because the ZIP files is 2.5 MB and the CSV file uncompressed it 25 MB.
Camsoft
A: 

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

just somebody
A: 

It seems that the problem was due to a missing fclose call before I tried to open the ZIP file.

Camsoft