龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > php编程 >

PHP 大日志文件读写

时间:2014-07-22 14:51来源: 作者: 点击:
分享到:
日志文件较大,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;
精彩图集

赞助商链接