<?php<BR>/**<BR> * 写入数据<BR> * @param [string] $path [文件路径]<BR> * @param [string] $mode [文件打开模式]<BR> * @param [string] $data [数据]<BR> * @return [bool] <BR> */<BR>function writeData($path, $mode, $data){<BR> $fp = fopen($path, $mode);<BR> $retries = 0;<BR> $max_retries = 100;<BR> do {<BR> if ($retries > 0) {<BR> usleep(rand(1, 10000));<BR> }<BR> $retries += 1;<BR> }while (!flock($fp, LOCK_EX) and $retries <= $max_retries);<BR> if ($retries == $max_retries) {<BR> return false;<BR> }<BR> fwrite($fp, $data."\r\n");<BR> flock($fp, LOCK_UN);<BR> fclose($fp);<BR> return true;<BR>}</P><P><BR>/**<BR> * 读数据<BR> * @param [string] $path [文件路径]<BR> * @param [string] $mode [文件打开模式]<BR> * @return string <BR> */<BR>function readData($path,$mode){<BR> $fp = fopen($path, $mode);<BR> $retries = 0;<BR> $max_retries = 100;<BR> do {<BR> if ($retri<div>本文来*源gaodai^.ma#com搞#代!码网</div><pre>搞gaodaima代码
es > 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+’);