我写的是一个统计,读取与写入。可是数据好像经常出错,不知哪里写错了。比如明明total.txt里数据是3030,可有时候再去刷新,马上又变成1,开始增加了,好奇怪。pass.txt也是经常出错。total.txt与pass.txt里面的数字为什么不能一直增加?老是会回到1再开始增加?<?php$myfile = fopen("total.txt", "r");
$total = fread($myfile,filesize("total.txt"));
fclose($myfile);
$myfile = fopen("pass.txt", "r");
$pass = fread($myfile,filesize("pass.txt"));
fclose($myfile);if($_GET['score']){
$myfile = fopen("record.txt", "a");
$ip = $_SERVER['REMOTE_ADDR'];
$time = date("Y-m-d H:i:s",time()+8*3600);
$score = $_GET['score'];
$ttime = $_GET['ttime'];
    $txt = "$time,$ip,$score,$ttime\n";
if(!empty($ttime)){
    fwrite($myfile, $txt);
}
fclose($myfile);

$myfile = fopen("total.txt", "w");
$total = $total + 1;
    fwrite($myfile, $total);
    fclose($myfile);

 if($score > 59){
    $myfile = fopen("pass.txt", "w");
$pass = $pass + 1;
    fwrite($myfile, $pass);
    fclose($myfile);
 }
//echo $txt;
exit;
}
?>

解决方案 »

  1.   

    输出看下什么问题echo $total;
    echo '<br/>';
    $total = $total + 1;
    echo $total;
    echo '<br/>';
        fwrite($myfile, $total);
        fclose($myfile);
      

  2.   

    考虑下网站上有两个以上的用户同时访问total.txt用户1执行$myfile = fopen("total.txt", "r");,这个时候total.txt只读用户2已经执行到$myfile = fopen("total.txt", "w");,文件打开失败,返回NULL$total=NULL+1=1,fwrite($myfile, $total);得到的就是1
      

  3.   

    利用php多用户读写文件要怎么修改我的代码?
      

  4.   

    http://tianminqiang.blog.163.com/blog/static/1343744882013276224269/