PHP File: Locking and Unlocking File
PHP File Tips - Part 12: You can lock a file use flock() function. This function allows you to performe a simple reader/write model. Once a file is locked with flock(), other processing attempting to write to file have to wait until unlock. It avoid multiprocess at a file that can corrupt it.
<?php
$f = "test2.txt";
$fo = fopen($f, "wb+") or die("cannot open");
if(flock($fo, LOCK_EX)){
fwrite($fo,"test add a 2 string") or die("cannot write to file");
flock($fo, LOCK_UN);
}else{
die("Cannot lock file");
}
fclose($fo);
echo "sucess";
?>
