I am locking files but then when I read them I cannot, my code...
// 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';
}
}
What is going on here?
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 ...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.