Renaming a php file while running - is blocking possible?

I will need to make changes to the file phpat runtime.
This is a configuration file that, in an emergency, needs to change one of its parameters.
My question is, is it possible, before writing a file lock, that other sessions trying to access this file are delayed until the file is overwritten, and not the session crashes, saying that the file was not found?

+3
source share
1 answer

Yes, see instruction for flock function

Example from the manual:

<?php

$fp = fopen("/tmp/lock.txt", "r+");

if (flock($fp, LOCK_EX)) {  // acquire an exclusive lock
    ftruncate($fp, 0);      // truncate file
    fwrite($fp, "Write something here\n");
    fflush($fp);            // flush output before releasing the lock
    flock($fp, LOCK_UN);    // release the lock
} else {
    echo "Couldn't get the lock!";
}

fclose($fp);

?>

-, , , (FAT) flock(), false. PHP ISAPI, flock / PHP, .

+3

All Articles