Home > Backend Development > PHP Tutorial > PHP large log file reading and writing

PHP large log file reading and writing

WBOY
Release: 2016-07-25 09:06:47
Original
1291 people have browsed it
The log file is large, more than 2G. There is no way to use file operation, only read line by line.                                                                                                                       
                                                                                                                                                                                                                                                                                                       



set_time_limit(0);
@ini_set('memory_limit', '64M');
    $conn = mysql_pconnect('localhost', 'hcq', 'hcq') or die("con't connection db .");
  1. mysql_select_db('log', $conn);
  2. mysql_query('set names utf8', $conn);
  3. $file = '/home/hcq/data/www.nimmin.com_20120601_access.log';
  4. $handle = fopen($file, "r") or die("can't open file {$file}");
  5. while($log = stream_get_line($handle, 8192, "n")) {
  6. $i = explode(' ', $log);
  7. $path = isset($i[6]) ? str_replace('http://www.nimmin.com', '', $i[6]) : false ;
  8. if($path) {
  9. preg_match('/(jpg|png|gif)/iu', $path, $m);
  10. if(isset($i[8]) && $i[8] == = '200' && count($m) > 0) {
  11. $len = isset($i[9]) ? intval($i[9]) : 0;
  12. $refer = isset($i[12] ) ? str_replace('"', '', $i[12]) : '';
  13. mysql_query('INSERT INTO trace(path, len, refer) VALUES (''.$path.'', '.$len .', ''.$refer.'')', $conn);
  14. }
  15. }
  16. }
  17. fclose ($handle);
  18. exit;
  19. Copy code
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template