What is the best Approach to keep the data between Database and A text file Synchronized?

Viewed 26

Currently , I'm writing a web application based on mysql and Php .

The thing is , the data in one of the tables should also be written to a text file to allow some process on the system to read from it .

All operations in the database are easy to perform unlike the text file when I have to update a specific value in one of the lines I have to write a function like this one:

function extendSrv($username,$duration,$fileDir)
{
    $date = "enddate=";
    $date .= setExpireDate($duration); //using some other function
    $Read = file($fileDir);

    foreach ( $Read as $LineNum => $line) {
        $LineParts = explode(' ',$line);
        if ( $LineParts[1] == $username ) {
            $LineParts[4] = $date;
            $Read[$LineNum] = implode(' ',$LineParts);
            break;
        }
    }
    
    file_put_contents($fileDir,$Read,LOCK_EX);
}

So, Is what I'm doing correct to separate the database and text file modifications or is there another better approach to deliver the data from the database to the process ?

P.S: I don't have control on the process other than including the text file in the config !

Regards

0 Answers
Related