php读取超大文件fseek

function readMaxFile($fp , $start = 0)
{
   $tag = "\n";
   $i = 0;
   $content = '';
   while($i < 20)
   {
       if (feof($fp))
       {
          return 0;
       }
       fseek($fp, $start, SEEK_SET);
       $res = fread($fp, 1);
       $content .= $res;
       if (substr($content, -strlen($tag)) == $tag)
       {
           $i++;
           echo  $i." ->+++这里我插入到数据库+++"."\n";
       }
       $start+=1;

     if (feof($fp))
     {
          return 0;
     }
   }
    sleep(3);
    echo "从位置".$start."开始读取";
   return $start;
}
$fp = fopen("install.log", "r+");


 $re = readMaxFile($fp, 0);
 for($i=0; $i<100; $i++)
 {
     if ($re==0)
     {
           echo 'a函数返回0了循环结束';
           break;
     }
     $re = readMaxFile($fp, $re);
 }
 fclose($fp); 
echo "程序结束";

 

你可能感兴趣的:(php读取超大文件fseek)