The code for PHP to append data to a text file is as follows: & nbsp; The PHP file is accessed 5000 times in one minute, and $ word data is appended to the text file every time $ word & quot; OK! & Quot; ask experts if they do not lock the append data. after all, it is not a read, but an attempt to append data & nbsp; $ word & quot; OK! & Quot; & nbsp; PHP append data to a text file
The code is as follows:
// The php file is accessed 5000 times in one minute, and $ word data is appended to the text file every time. $ word = "OK! ";
// Ask your experts if they do not lock the append data. after all, it is not a read, but an attempt to append the data.
$ Word = "OK! ";
$ Fn = 'a2.txt ';
$ Fp = fopen ($ fn, 'A ');
Flock ($ fp, LOCK_EX); // lock the file to avoid reading/writing
Fwrite ($ fp, $ word );
Flock ($ fp, LOCK_UN); // unlock
Fclose ($ fp); // close the program stream
If it does not affect, I don't want to lock it .....
------ Solution --------------------
Lock (lock_sh) during read and lock (lock_ex) during write, so that the program will not encounter problems
------ Solution --------------------
File_put_contents ($ fn, $ word, LOCK_EX );
------ Solution --------------------
On versions of PHP before 5.3.2, the lock is released also by fclose () (which is also called automatically when script finished ).
Fclose () already contains the lock.
Additional operations!