-1

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.

Daniel
  • 2,355
  • 9
  • 23
  • 30

1 Answers1

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