Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
191 views
in Technique[技术] by (71.8m points)

PHP Read Large text file line by line and store into array

I am trying to parse a large log file and store data to array. The above test code works fine on localhost, but on server it returns "The connection was reset" error on firefox and "ERR_EMPTY_RESPONSE" on Chrome.

Is there a way to optimize this script in order to run on server too?

<?php
ini_set('max_execution_time', 990);
ini_set("memory_limit","1024M");
ini_set('max_input_vars', 3000);
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

set_time_limit(200);

function startsWith ($string, $startString){
    $len = strlen($startString);
    return (substr($string, 0, $len) === $startString);
}

function append_string ($str1, $str2){
    $str1 .=$str2;
    return $str1;
}

function readTheFile($file) {
  $i = 0;
  $handle = fopen($file, "rb") or die("Unable to open file!");

  while(!feof($handle))  {
      $line = fgets($handle);
      if(!startsWith($line,"::1") && !startsWith($line,"127.0.0.1") || $line != ''){
        $line = str_replace('"', '', explode (' ', $line));
        $line = str_replace('[', '', $line);
        $line = str_replace(']', '', $line);
        if(array_key_exists("5",$line) && array_key_exists("8",$line)){
          if($line[5] == "GET" || $line[5] == "POST" || $line[5] == "PUT" || $line[5] == "HEAD" || $line[5] == "DELETE" || $line[5] == "PATCH" || $line[5] == "OPTIONS"){
           if(is_numeric($line[8])){
              $dt = explode(":", $line[3], 2);
              $date = $dt[0];
              $time = substr($dt[1], 0, strrpos( $dt[1], ':'));
              $time = substr($time, 0, strrpos($time, ':'));
              $time = append_string($time, ':00');


              $recs[$i]['Date']  = $date;
              $recs[$i]['Time']  = $time;
              $recs[$i]['Method']   = $line[5];
              $recs[$i]['StatusCode'] = $line[8];


              unset($dt);
              unset($date);
              unset($time);
              $i++;
          }
        }
      }
    }
    unset($line);
  }

  fclose($handle);
  return $recs;
}
$records = [];

$records = readTheFile('access.log');
echo json_encode($records);
?>
question from:https://stackoverflow.com/questions/65871158/php-read-large-text-file-line-by-line-and-store-into-array

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...