PHP 大日志文件读写
日志文件较大,2个多G。没办法用file操作,只能按行读取。
日志文件较大,2个多G。没办法用file操作,只能按行读取。
set_time_limit(0); @ini_set('memory_limit', '64M'); $conn = mysql_pconnect('localhost', 'hcq', 'hcq') or die("con\'t connection db."); mysql_select_db('log', $conn); mysql_query('set names utf8', $conn); $file = '/home/hcq/data/www.nimmin.com_20120601_access.log'; $handle = fopen($file, "r") or die("can\'t open file {$file}"); while($log = stream_get_line($handle, 8192, "\n")) { $i = explode(' ', $log); $path = isset($i[6]) ? str_replace('http://www.nimmin.com', '', $i[6]) : false; if($path) { preg_match('/(jpg|png|gif)/iu', $path, $m); if(isset($i[8]) && $i[8] === '200' && count($m) > 0) { $len = isset($i[9]) ? intval($i[9]) : 0; $refer = isset($i[12]) ? str_replace('"', '', $i[12]) : ''; mysql_query('INSERT INTO trace(path, len, refer) VALUES (\''.$path.'\', '.$len.', \''.$refer.'\')', $conn); } } } fclose ($handle); exit;
- 上一篇:两句话将xml字符串转换成数组.
- 下一篇:php获取文件创建时间、修改时间
精彩图集
精彩文章