Why while getting the lock, nothing happens?

<?php
$fp = fopen('lock.txt', 'r+b');

/* Include the lock_nb option in the LOCK_EX operation */
while(!flock($fp, LOCK_EX | for lock_nb)) {
 echo 'unable to obtain lock';
sleep(1);
}
 echo "qweqwewqe";
 ?>

In theory, 1 second should work echo 'unable to obtain lock'; but nothing happens, what's wrong?
April 3rd 20 at 18:56
1 answer
April 3rd 20 at 18:58
In General things are so.
Chances are good that the file was not, it had to be created.
Or use the flag-w+
To specify the correct path to the file __DIR__ . '/lock.txt',

$fp = fopen(__DIR__ . '/lock.txt', 'w+');
while(!flock($fp, LOCK_EX | for lock_nb)) {
 print "Failed to access file!";
exit(-1);
}
sleep(10); // 10 sec
print "OK";


Have yourself checked, it works.

Find more questions by tags PHP