Lock a text database or file before writing data into it using php
Lock a text database or file before writing data into it using php
This simple tutorial shows how to lock a text database or file before writing data into it using php, usually when a developer uses text database instead of SQL or MYSQL or DB2 for his website, many will come across issues due to simultaneous insertion of data by their users.The text database doesn’t have any logic unlike the others.So to prevent these kind of issues we can lock the text database for writing data using flock() in php.
< ?php $file = fopen('file.txt', 'w'); if(flock($file, LOCK_EX | LOCK_NB)){ echo 'Got lock, continue writing to file'; // Code to write to file //Unlock the file once writing is done flock($file,LOCK_UN); }else{ echo 'File is locked by another process, aborting writing'; // Couldn't obtain the lock immediately } ?>Lock a text database or file before writing data into it using php,