I have a log file which keeps on updating every minute which also has an auto increment value. For the first time, I will read the data and insert into database along with the last auto increment value in that file. For the second time, I am able to get the last auto increment value from the database but couldn't able to check that value in the file and removing the remaining lines less than that value in the file.
Asked
Active
Viewed 26 times
-1
-
can you show the content of file? – Alihossein shahabi Jun 05 '18 at 17:21
-
Thanks for reply, below is the content of a file. [Fri Aug 12 20:37:04 2016][780] abc abcded abcded where 780 is the auto increment value – naga lakshmi Jun 05 '18 at 17:26
-
Any update on the above? – naga lakshmi Jun 05 '18 at 17:50
1 Answers
0
i hope this code useful for you:
laravel.log :
[Fri Aug 12 20:37:04 2016][781] abc abcded1
[Fri Aug 12 20:37:04 2016][782] ali shahabi2
[Fri Aug 12 20:37:04 2016][783] mohammad3
[Fri Aug 12 20:37:04 2016][784] sara4
[Fri Aug 12 20:37:04 2016][785] reza5
code :
<?php
$search_id = 782; # last id in table
$handle = @fopen('laravel.log', "r");
$save_flag=0;
if ($handle)
{
while (!feof($handle))
{
$buffer = fgets($handle);
if ($save_flag==0)
{
if(strpos($buffer, "[$search_id]") !== FALSE)
$save_flag=1;
// save this line to DB
}
else
{
// All messages in this section are new
// so save this line to DB
}
}
fclose($handle);
}

Alihossein shahabi
- 4,034
- 2
- 33
- 53