时间:2021-07-01 10:21:17 帮助过:11人阅读
function read($filename) {
$fp = fopen($filename, 'rb');
flock($fp, LOCK_SH);
$data = @fread($fp, @filesize($filename));
fclose($fp);
return $data;
}
function write($filename, $data) {
$fp = fopen($filename, 'ab');
flock($fp, LOCK_EX);
fwrite($fp, $data);
fclose($fp);
return mt_rand(1, 999);
}
$file = './wr.txt'; //原文件是空的
echo 'r1: ', read($file), '|
';
echo 'w1: ', write($file, 'a'), '|
';
echo 'r2: ', read($file), '|
';
echo 'w2: ', write($file, 'b'), '|
';
echo 'r3: ', read($file), '|
';
?>
r1: |
w1: 745|
r2: |
w2: 404|
r3: |
function read($filename) {r1:
$fp = fopen($filename, 'rb');
flock($fp, LOCK_SH);
clearstatcache(); //清除文件状态缓存
$data = @fread($fp, @filesize($filename));
fclose($fp);
return $data;
}
function write($filename, $data) {
$fp = fopen($filename, 'ab');
flock($fp, LOCK_EX);
fwrite($fp, $data);
fclose($fp);
return $data;//mt_rand(1, 999);
}
$file = './wr.txt'; //原文件是空的
file_put_contents($file, ''); //清空源文件
echo 'r1: ', read($file), '
------解决方案--------------------
';
echo 'w1: ', write($file, 'a'), '
------解决方案--------------------
';
echo 'r2: ', read($file), '
------解决方案--------------------
';
echo 'w2: ', write($file, 'b'), '
------解决方案--------------------
';
echo 'r3: ', read($file), '
------解决方案--------------------
';
readfile($file); //显示一下