Php uses multiple processes to simultaneously control file read/write. Copy the code as follows :? Php *** write data ** @ param [string] $ path [file path] * @ param [string] $ mode [file open mode] * @ param [string] $ data [data] * @ return [B
The code is as follows:
/**
* Write Data
* @ Param [string] $ path [file path]
* @ Param [string] $ mode [file opening mode]
* @ Param [string] $ data [data]
* @ Return [bool]
*/
Function writeData ($ path, $ mode, $ data ){
$ Fp = fopen ($ path, $ mode );
$ Retries = 0;
$ Max_retries = 100;
Do {
If ($ retries> 0 ){
Usleep (rand (1, 10000 ));
}
$ Retries + = 1;
} While (! Flock ($ fp, LOCK_EX) and $ retries <= $ max_retries );
If ($ retries = $ max_retries ){
Return false;
}
Fwrite ($ fp, $ data. "\ r \ n ");
Flock ($ fp, LOCK_UN );
Fclose ($ fp );
Return true;
}
/**
* Read Data
* @ Param [string] $ path [file path]
* @ Param [string] $ mode [file opening mode]
* @ Return string
*/
Function readData ($ path, $ mode ){
$ Fp = fopen ($ path, $ mode );
$ Retries = 0;
$ Max_retries = 100;
Do {
If ($ retries> 0 ){
Usleep (rand (1, 10000 ));
}
$ Retries + = 1;
} While (! Flock ($ fp, LOCK_SH) and $ retries <= $ max_retries );
If ($ retries = $ max_retries ){
Return false;
}
$ Contents = "";
While (! Feof ($ fp )){
$ Contents. = fread ($ fp, 8192 );
}
Flock ($ fp, LOCK_UN );
Fclose ($ fp );
Return $ contents;
}
WriteData ('d:/webServer/demo.txt ', 'A +', 'This is a Demo ');
Echo readData ('d:/webServer ', 'R + ');
The http://www.bkjia.com/PHPjc/736838.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/736838.htmlTechArticle code is as follows :? Php/*** write data ** @ param [string] $ path [file path] * @ param [string] $ mode [file open mode] * @ param [string] $ data [data] * @ return [B...