Thanks for explaining it (much better than the manual!).
Since the file is used only by one app and only one script, the flock solution will work. And I need to ensure a half written file isn't being read. My understanding is that this will correctly work only when handling the file using a file handle. How would you recommend changing the following?
$some_array = file("filename");
I would think something like:
$fp = fopen("filename","r");
flock($fp, LOCK_SH);
$contents = fread($fp, filesize("filename"));
fclose($fp);
$some_array = explode("\n",$contents);
Though, the manual states that using file (or file_get_contents) is better for performance.
For the process which is writing, I have:
$fd = fopen("filename", "w");
flock($fd,LOCK_EX); // trying to use file lock
ftruncate($fd,0);
fseek($fd,0);
fwrite($fd, "new data here");
fclose($fd);
Am I missing anything for a successful lock and safe data integrity?