您是否知道如何通过从zip文件中读取CSV文件来提高性能?
首先打开zip文件,然后将数据放入内存,然后通过fgetcsv
$zip = new ZipArchive();
if ($zip->open($fileName)) {
$info = $zip->statIndex(0);
$fp = $zip->getStream($info['name']);
if(!$fp) exit("failed\n");
while (!feof($fp)) {
$contents .= fread($fp, 2);
}
fclose($fp);
$zip->close();
}
$temp = fopen("php://memory", "rw");
fwrite($temp, $contents);
fseek($temp, 0);
while (($data = fgetcsv($temp, 0)) !== false) {
....
}
答案 0 :(得分:3)
使用php手册快速检查表明这应该有效:
<?php
$fp = fopen('zip://test.zip#test', 'r'); // test name of file in archive
if (!$fp) {
exit("cannot open\n");
}
while (($data = fgetcsv($fp, 0)) !== false) {
...
}
fclose($fp);