PHP file processing, possible data corruption
one text
Solution:
If Script A writes data into the first row while Script B is currently reading it, you might indeed end up with corrupted data for that line. The chances of that happening depends on the frequency of these operations. It's likely slim. But in theory, this could happen.
The easiest way to protect against that is to use
Your script writing to the file has to acquire an exclusive lock when it wants to write to the file. Your script reading the file will have to check whether the file is currently locked. If it is, the script can wait for the file to be released again.
Note that this approach assumes that the file will only ever be changed by these two PHP scripts.
Source