当前位置:Gxlcms > PHP教程 > 文件读写顺序有关问题

文件读写顺序有关问题

时间:2021-07-01 10:21:17 帮助过:11人阅读

文件读写顺序问题
本帖最后由 o911016 于 2013-03-13 12:45:07 编辑



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: |


根据结果发现,执行顺序和PHP语句的顺序不同,
实际上的顺序是“r1 -> r2 -> r3 -> w1 -> w2”。
我试过把读文件所加的锁LOCK_SH改成LOCK_EX,结果还是和上面的顺序一样。

怎样才能让读写顺序符合语句顺序“r1 -> w1 -> r2 -> w2 -> r3”来执行?


------解决方案--------------------
本帖最后由 xuzuning 于 2013-03-13 12:56:42 编辑

真正的原因是文件状态缓存造成 filesize($filename) 始终为 0
function read($filename) {
$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); //显示一下
r1:
------解决方案--------------------

w1: a
------解决方案--------------------

r2: a

人气教程排行