php 读文件并发,PHP并发读写文件问题 高手请进!

ajax1.html三个ajax方法同时访问ajax1.php,ajax1.php中有进行文件data.php读写,由于三个ajax方法访问频率特别高,就产生了并发访问,导致读写出错,使用了flock()还是会出错,请高手们指导一下怎么解决呢?

ajax1.html代码:

var a = 1;

var b = 1;

var c = 1;

function ajax1(){

$.get('ajax1.php?from=a&value='+a, function(res){

$('#ajax1').text(a);

a++;

if(res == 1){

ajax1();

}

});

}

function ajax2(){

$.get('ajax1.php?from=b&value='+b, function(res){

$('#ajax2').text(b);

b++;

if(res == 1){

ajax2();

}

});

}

function ajax3(){

$.get('ajax1.php?from=c&value='+c, function(res){

$('#ajax3').text(c);

c++;

if(res == 1){

ajax3();

}

});

}

function beginAjax(){

ajax1();

ajax2();

ajax3();

}

ajax1.php代码:

$from = $_GET['from'];

$value = $_GET['value'];

$data = is_array(include 'data.php')? include 'data.php': array();

$data[] = $from .'-'. $value;

$file = fopen('data.php', 'w');

$lock = flock($file, LOCK_EX);

if($lock){

fwrite($file, '<?php ');

fwrite($file, PHP_EOL);

fwrite($file, 'return ');

fwrite($file, var_export($data, true));

fwrite($file, ';');

flock($file, LOCK_UN);

}

fclose($file);

exit('1');

data.php代码(以下数据是出错了的数据):

return array (

0 => 'b-3',

);1 => 'a-1',

2 => 'c-1',

3 => 'b-2',

4 => 'a-2',

5 => 'c-2',

);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值