The SplFileObject flock() function is an inbuilt function of Standard PHP Library (SPL) in PHP which is used to apply portable lock on the file.
bool SplFileObject::flock( $opr, $isBlock )
Parameters: This function accept two parameters as mentioned above and described below:
$opr: It is used to specify the operation to be apply from the following list:
- LOCK_SH: Acquire a shared lock (reader).
- LOCK_EX: Acquire an exclusive lock (writer).
- LOCK_UN: Release a lock.
- LOCK_NB: Not block while locking.
$isBlock: This parameter is set to True if the lock is block.
Return values: This function returns True on on success or False on failure.
Note: Make sure the used file in below program named as gfg.txt and should have read and write permissions.
Below Programs illustrate the SplFileObject::flock() function in PHP:
Success Lock and Unlock Operation.
Success Exclusive Lock and Unlock Operation
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.