如何下载并解压压缩的 CSV 并将 CSV 文件的内容放入 MySQL 表中
我正在尝试编写一个 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.');
}
上面的代码确实设法将文件下载到服务器上具有唯一名称的目录中。但我无法提取内容。
我尝试使用 PHP 的 zip_open
命令来提取 zip,但它总是返回错误代码 19
而不是句柄。我检查了传递给 zip_open 函数的路径,它是完整的系统路径,即 /path/to/zip/save/folder/data-5384e2306718492958f20e68de95c6fa.zip
。
注意:
CSV 文件压缩后大小为 2.5 MB,未压缩时大小为 30 MB。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
请记住在将内容保存为文件之前删除响应标头。另外,您可以检查是否有 HTTP/1.1 200 OK 响应,或 301/302 重定向跟随
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() 错误号 19 是:“Zip 文件函数错误:不是 zip 存档”。这意味着您的脚本没有正确下载 zip 文件。
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 = '数据-' 。 md5(microtime()) 。 '。拉链';
$url="http://www.some.zip";
$target = 'data-' . md5(microtime()) . '.zip';
我用这个
你应该可以从那里拿起它
I use this
you should be able to pick it up from there
看来问题是由于在我尝试打开 ZIP 文件之前缺少
fclose
调用造成的。It seems that the problem was due to a missing
fclose
call before I tried to open the ZIP file.