flock() is used to lock a file so that two or more people do not get access to it at the same time. This helps protect the file from being corrupted.
flock() takes two arguments: a file handler and a lock type.
flock() does not work with NFS or other networked file systems or with such as FAT that does not support file locking.
||Reading lock. Others can read file.|
||Exclusive lock. The file cannot be opened by others.|
||If a file is already locked by another user,
The code below shows how we should change Files/Solutions/AddEntry.php to protect Employees.txt from being corrupted.
---- C O D E O M I T T E D ---- flock($myFile, LOCK_EX); fwrite($myFile,$outputString); flock($myFile, LOCK_UN); fclose($myFile); ---- C O D E O M I T T E D ----