I ask the question yesterday: Ajax Php get file from server, how to detect the file is open
There is this case. I have many files(data) in the server. I use Ajax and php for Client browser to get the data from the server.
When a person opens a website and accesses a file from the server, at the same time, if another person(user) open this website, I want to find a way to detect that file is open, if so, I can load a different file for her.
I think this is not duplicate from PHP check if file is in use
Finally, I find a way to solve this:
when a person open the website, he assess one file from php(ajax), and in another directory he create a tmp file, when he finished (after clicking a button), I delete those tmp file. So Another person coming, he can check if the same name tmp file exists, if so, he could not open it. and get another file.(create a tmp file).
According to this idea, I first to get the fileName list from the server directory, and then random generate a file name, and then use ajax to check whether this file is exist or not.Problem is I can not get the status from the php
why? Any suggestion is appreciated, thanks!
If not exist, flag = false, so I can load this file(when loading a file, I create a tmp file with the same name).
ajax for get file name list, generate a file and check the file exist status
:
$.ajax({
type: "GET",
url: "getXmlFileName.php", //getXmlFileName.php
cache: false,
success: function(result){
if(result instanceof Array)
{
if(result.length!==0)
{
var randomnumber;
var flag = true;
while(flag) {
randomnumber = Math.floor(Math.random() * result.length);
str = result[randomnumber];
$.ajax({
url: 'checkFileStatus.php',
type: "POST",
data: {
name: str
},
dataType: "json",
success: function (data) {
console.log(data);
flag = data;//Here I can not get the status, Why?
//flag = true; But If I change to this
},
error: function (xhr, textStatus, errorThrown) {
console.log('ajax saving file error...');
//flag = false; But If I change to this, I can get the value, Why?
return false;
}
});
}
loader = new XMLLoader(str);
loader.load(str);
}
}
}
});
getXmlFileName.php
<?php
$files = array();
$dir = getcwd();
$dir = dirname($dir); //get Parent directory
$dir .= "/XMLdata"; //jsondata
// Open a directory, and read its contents
if (is_dir($dir)){
if ($dh = opendir($dir)){
while (($file = readdir($dh)) !== false){
if ($file == '.' || $file == '..') {
continue;
}
$files[] = $file;
}
closedir($dh);
}
}
header('Content-type: application/json');
echo json_encode($files);
?>
'checkFileStatus.php'
<?php
header("Content-type: application/json");
$file_x ="./tmpData/";
$file_x .= $_POST['name'];
if (file_exists($file_x))
{
echo true;
}
else
{
echo false;
}
?>
XMLLoader.load function:
load: function (url) {
var _this = this;
$.ajax({
type: "GET",
url: url,
dataType: "text",
success: function (xml) {
if (typeof xml == 'string' || xml instanceof String) {
xml = xml.replace(/\\'/g, "'"); //dataType: "text",
xml = xml.replace(/\\"/g, '"'); //dataType: "text",
_this.parse($(xml));
}
}
});
this.saveTmpData();
},
saveTmpData
saveTmpData: function(){
var _this = this;
$.ajax({
url: 'createTmpFile.php',
type: "POST", // type should be POST
data: {
name: _this.fileName
}, // send the string directly
dataType: "json",
success: function (data) {
console.log(data);
return true;
},
complete: function () {
},
error: function (xhr, textStatus, errorThrown) {
console.log('ajax saving json file error...');
return false;
}
});
},
createTmpFile.php
<?php
header("Content-type: application/json");
$file_x ="./tmpData/";
$file_x .= $_POST['name'];
$createFile = touch($file_x);
echo (file_exists($file_x));
?>
Problem Solved
It seems I can continue,
If I change code below, although I do not know why? Can anyone interpret this?
$.ajax({
url: 'checkFileStatus.php',
type: "POST",
data: {
name: str
},
dataType: "json",
success: function (data) {
flag = true; But If I change to this
},
error: function (xhr, textStatus, errorThrown) {
flag = false;
}
});