Hello,
I have a file on my website which contains some SERIALIZEd data. Every time a certain page is accessed, the file's contents are UNSERIALIZEd into an array, a counter within the array is incremented, and the data is eventually SERIALIZEd and saved back into the file. Recently, the file's contents were blanked somehow. So I decided to protect it. I don't know much about file locking etc., but could someone let me know whether the following solution is adequate protection:
ignore_user_abort(true);
$fp = fopen($file,"w") or die("Unable to open $file");
flock($fp,LOCK_EX);
// write data
flock($fp,LOCK_UN);
fclose($fp);
ignore_user_abort(false);
I am not quite sure how file locking works. For example, if a script is attempting to FOPEN a file which has been locked whilst writing, does it simply pause until the lock is removed, or generate an error, or what?
Thank you in advance for any enlightenment!