解析制表符分隔txt文件PHP fgetcsv挂起/不结束/退出函数

时间:2014-02-11 21:05:12

标签: php mysql while-loop fgetcsv

这个函数非常通用,用于解析用tab分隔的txt文件,我试图解析的文件是geonames数据库,每次都在1953146结果时达到顶峰,从那时起根本没有任何事情发生,没有更多的查询并没有退出,计算行我可以看到文件中有8,000,000行所以我猜它是停滞,错误被启用并且没有错误返回php_memory设置为2048M执行时间设置为无限。

<?php

function table_populate($table,$file,$columns){
    $handle = fopen($file, "r");
    $lines = count(explode("\n",file_get_contents($file)));
    $i = 0;
    while (($line = fgetcsv($handle, 10000, "\t")) !== false && $i < $lines) {
        if(preg_match('[#]',$line['0'])){
          // do nothing row is commented out
        }else{
          $row = '';
          $comma = '';
          for ($z = 0; $z < count($line); $z++) {
            $row .=  $comma."'".$line[$z]."'";
            $comma = ', ';
          }
          $sql = "INSERT INTO ".$table." (".$columns.") VALUES (".$row.")";

        }
        $i++;
    }   
    fclose($handle);
    return;
}

?>

0 个答案:

没有答案