热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

1ajax1.html三个ajax方法同时访问1ajax1.php,1ajax1.php中有进行文件1data.php读写,由于三个ajax方法访问频率特别高,就产生了并发访问,导致读写出错,使用了1

1
ajax1.html

三个ajax方法同时访问




1
ajax1.php

,

1
ajax1.php

中有进行文件




1
data.php

读写,由于三个ajax方法访问频率特别高,就产生了并发访问,导致读写出错,使用了

1
flock()

还是会出错,请高手们指导一下怎么解决呢?

ajax1.html代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
$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, '
    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代码(以下数据是出错了的数据):

1
2
3
4
5
6
7
8
return array (

  0 => 'b-3',

);1 => 'a-1',

  2 => 'c-1',

  3 => 'b-2',

  4 => 'a-2',

  5 => 'c-2',

);



   



推荐阅读
author-avatar
大坑啊同志
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有