Last updated on February 3rd, 2022 at 07:14 pm

Usually while using plain text as database we need to take care of lot of things.In that the main point to be always noted is if one request is using the text DB then we need to wait for that request to complete and then move ahead to process the next, otherwise data loss may happen.
Why DATA LOSS happens?
The text db is not having that intelligence to queue up the request and process it one by one,
So here we can use FLOCK in php to prevent that data loss.

$file = fopen('myfile.txt', 'w');

if(flock($file, LOCK_EX | LOCK_NB)){
    echo 'Got lock, continue writing to file';
    // Code to write to file
}else{
    echo 'File is locked by another process, aborting writing';
    // Couldn't obtain the lock immediately
}

I always recommend my users to use MYSQL as the database if your website has more traffic.

Leave a Reply

Your email address will not be published. Required fields are marked *