简体   繁体   English

如何下载并解压缩CSV并将CSV文件的内容保存到MySQL表中

[英]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. 我正在尝试编写PHP脚本,以从包含单个CSV文件的Web服务器下载zip文件,然后将提取的CSV文件的内容加载到现有的MySQL数据库表中。

$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. 我尝试使用PHP的zip_open命令提取zip,但它总是返回错误代码19而不是句柄。 I've checked the path that I pass to the zip_open function and it's a full system path ie /path/to/zip/save/folder/data-5384e2306718492958f20e68de95c6fa.zip . 我已经检查了传递给zip_open函数的路径,它是完整的系统路径,即/path/to/zip/save/folder/data-5384e2306718492958f20e68de95c6fa.zip

Note: 注意:

The CSV file file is 2.5 MB compressed and 30 MB uncompressed. CSV文件文件已压缩2.5 MB,未压缩30 MB。

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 另外,您可以检查是否存在HTTP / 1.1 200 OK响应,或者是否遵循301/302重定向

zip_open() error number 19 is: "Zip File Function error: Not a zip archive". zip_open()错误号19是:“ Zip文件功能错误:不是zip存档”。 That means your script did not download the zip file properly. 这意味着您的脚本未正确下载zip文件。

$url="http://www.some.zip"; $ url =“ http://www.some.zip”; $target = 'data-' . $ target ='数据-'。 md5(microtime()) . 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 你应该能够从那里拿起它

看来问题出在我尝试打开ZIP文件之前,是因为缺少fclose调用。

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

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