Look at the example below
flock($fp, LOCK_EX); // do an exclusive lock
fwrite($fp, "Write something here\n");
flock($fp, LOCK_UN);
What happens if an exclusive lock on the file is already being hold by another user? Does this script suspend until the other user leaves the lock?
If not, what should I do in order to wait until it's allowed to write to the file?
OS: Windows, Unix
File Locking in PHP: Do a LOCK_EX request "suspend" until ok
Moderator: General Moderators
Re: File Locking in PHP: Do a LOCK_EX request "suspend" until ok
the answer comes from the inquirer himself;
yes it does. to test it out run the script below twice simultaneously.
yes it does. to test it out run the script below twice simultaneously.
Code: Select all
<?php
$fp = fopen("ftest.txt", "w");
if (flock($fp, LOCK_EX)) {
echo "Got it!\n";
sleep(8);
flock($fp, LOCK_UN);
}
?>