PHP flock () behavior on Windows vs Linux

I am executing the same piece of code on a Windows machine and a Linux machine. Both work with PHP 5.4:

$file = "lock.txt";
$fp = fopen($file, "w+");
flock($fp, LOCK_EX);
var_dump(@file_put_contents($file, 'hello' . rand()));
flock($fp, LOCK_UN);
fclose($fp);

On a Windows computer, the operation file_put_contents()fails and returns false. On a Linux machine, this succeeds.

I'm trying to figure out which of these two actions is really correct, and how I can standardize the behavior in both systems (for the record, my goal is failure file_put_contents(). I don’t need to be solid or atomic, I just want it to fail.)

I saw many other questions on this topic, but all of them seem to refer to several scripts accessing the same file, which is not the case here.

+1
source share
1

, flock() , , .

flock() Windows

,

Flock ADVISORY; .

, ; - : " , - flock() - , , ."

+1

Source: https://habr.com/ru/post/1629917/


All Articles