This article mainly introduces PHP to use multiple processes to control file read and write examples, need friends can refer to the following
The code is as follows: <?php/** * Write Data * @param [string] $path [File path] * @param [string] $mode [File open mode] & nbsp;* @param [string] $data [data] * @return [bool] */function WriteData ($pat H, $mode, $data) { $fp = fopen ($path, $mode); $retries = 0; &N Bsp $max _retries = 100; do { -($retries > 0) { uslee P (rand (1, 10000)); } $retries + 1; }while (!flock ($FP, LOCK_EX) and $retries <= $max _retries); if ($retries = = $max _retries) { return false; &NBSP} fwrite ($fp, $data. " RN "); flock ($fp, Lock_un); fclose ($FP); &Nbsp;return true; /** * read data * @param [string] $path [File path] * @param [string] $mode [File open mode] &n bsp;* @return string */function ReadData ($path, $mode) { $fp = fopen ($path, $mode); $retries = 0; $max _retries = 100; do { if ($retries > 0) { usleep (rand (1, 10000)); &N Bsp } $retries + 1; }while (!flock ($FP, lock_sh) and $retries <= $max _retries); if ($retries = = $max _retries) { return false; &NBSP;} $contents = ""; while (!feof ($fp)) { $contents. = Fread ($fp, 8192); &NBSP; flock ($fp, Lock_un); fclose ($FP); return $contents; } writedATA (' D:/webserver/demo.txt ', ' A + ', ' is a demo '); echo readdata (' D:/webserver ', ' r+ ');