PHP read only new added entry in file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
qasimkhans
Forum Newbie
Posts: 2
Joined: Mon Nov 03, 2014 10:21 am

PHP read only new added entry in file

Post by qasimkhans »

i have a log file which gets new data time by time. is there any way i can read only new added entries (block) in the file? instead of start reading file from start to the end of file. this file size will be in GBs soon. Thanks. currently i am using file() to read the file. following is the example of new added block in file.

----SMS_START----
recv_time:2014-10-09 18:32:39
Span: 1
From-Number: +1347XXXXXXX
Timestamp: 14/10/09 18:32:16 96
Type: PDU
SMS-SMSC-Number: +12404492163
Content: Thanks,

----SMS_END----
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: PHP read only new added entry in file

Post by Celauran »

Any reason you're not using a database instead of a file? Seems like a perfect use case.
qasimkhans
Forum Newbie
Posts: 2
Joined: Mon Nov 03, 2014 10:21 am

Re: PHP read only new added entry in file

Post by qasimkhans »

Celauran wrote:Any reason you're not using a database instead of a file? Seems like a perfect use case.

following is my script, i read the file and save in DB.

<?php
require_once('dbconnect.php');

$file = file("/var/log/asterisk/recvsms/recvsms_log");
foreach($file as $key => $value)
{
$filter_value = trim($value);

if($filter_value!="" && $filter_value!="----SMS_START----" && $filter_value!="----SMS_END----"){
$new_array[] = $value;
$my_array = array_chunk($new_array, 7);
}
}
$count = count($my_array);
for($I=0;$I<$count;$I++){
foreach($my_array[$I] as $key => $value){

$ss = explode(":", $value);
$mm_array[$I][$ss[0]] = str_replace($ss[0].":", "", $value);
}
mysql_query("insert into receive_sms (receive_time,span,sms_from_number,timestamp,type,sms_center_number,sms_content)
values('".$mm_array[$I]['recv_time']."','".$mm_array[$I]['Span']."','".$mm_array[$I]['FromNumber']."','".$mm_array[$I]['Timestamp']."','".$mm_array[$I]['Type']."','".$mm_array[$I]['SMS-SMSC-Number']."','".$mm_array[$I]['Content']."')");
}
?>

that log file generated by system. so i have to read data from that log file
Post Reply