1

我正在锁定文件,但是当我阅读它们时,我不能,我的代码......

// ACQUIRE READ LOCK
if(flock($file, LOCK_SH)) {
// READ HASHES FILE
if($contents = file('haasdas.txt')) {
    // RELEASE READ LOCK
    flock($file, LOCK_UN);
} else {
            echo 'errrrrrorzzzer';
      }
}

这里发生了什么?

4

1 回答 1

0

As the docs mention, flock() works not on a filename, but on a file descriptor:

$fd=fopen($filename,'rb');
while (!flock($fd,LOCK_SH)) usleep(500);
$fs=fstat($fd);
$contents=fread($fd,$fs['size']);
flock($fd,LOCK_UN);
fclose($fd);

error handling is left as an exercise to the reader ...

于 2012-05-24T05:56:15.197 回答