ajax1.html三個(gè)ajax方法同時(shí)訪問ajax1.php,ajax1.php中有進(jìn)行文件data.php讀寫,由于三個(gè)ajax方法訪問頻率特別高,就產(chǎn)生了并發(fā)訪問,導(dǎo)致讀寫出錯(cuò),使用了flock()還是會出錯(cuò),請高手們指導(dǎo)一下怎么解決呢?
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代碼(以下數(shù)據(jù)是出錯(cuò)了的數(shù)據(jù)):
return array (
0 => 'b-3',
);1 => 'a-1',
2 => 'c-1',
3 => 'b-2',
4 => 'a-2',
5 => 'c-2',
);
PHP并發(fā)讀寫文件問題 高手請進(jìn)!
呼啦一陣風(fēng)
2019-03-16 03:30:11