You can lock a file use flock() function. This function allows you to performance 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.
Code:
<?php
2 $f = "test2.txt";
3
4 $fo = fopen($f, "wb+") or die("cannot open");
5
6 if(flock($fo, LOCK_EX)){
7 fwrite($fo,"test add a 2 string") or die("cannot write to file");
8 flock($fo, LOCK_UN);
9 }else{
10 die("Cannot lock file");
11 }
12
13 fclose($fo);
14 echo "sucess";
15 ?>
Bookmarks