Javascript Ajax php文件控件

Javascript Ajax php文件控件,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我昨天问了一个问题: 有这样的情况。我在服务器中有许多文件和数据。我使用Ajax和php作为客户端浏览器从服务器获取数据 当一个人打开一个网站并从服务器访问一个文件时,同时,如果另一个人打开这个网站,我想找到一种方法来检测该文件是否打开,如果是,我可以为她加载不同的文件 我认为这不是重复的 最后,我找到了解决这个问题的方法: 当一个人打开网站时,他评估phpajax中的一个文件,并在另一个目录中创建一个tmp文件,当他点击一个按钮完成后,我将删除这些tmp文件。所以另一个人来了,他可以检查是否存

我昨天问了一个问题:

有这样的情况。我在服务器中有许多文件和数据。我使用Ajax和php作为客户端浏览器从服务器获取数据

当一个人打开一个网站并从服务器访问一个文件时,同时,如果另一个人打开这个网站,我想找到一种方法来检测该文件是否打开,如果是,我可以为她加载不同的文件

我认为这不是重复的

最后,我找到了解决这个问题的方法:

当一个人打开网站时,他评估phpajax中的一个文件,并在另一个目录中创建一个tmp文件,当他点击一个按钮完成后,我将删除这些tmp文件。所以另一个人来了,他可以检查是否存在同名的tmp文件,如果存在,他就无法打开它。并获取另一个文件。创建一个tmp文件

根据这个想法,我首先从服务器目录中获取文件名列表,然后随机生成一个文件名,然后使用ajax检查该文件是否存在。问题是我无法从php获取状态为什么?任何建议都将不胜感激,谢谢

如果不存在,flag=false,因此我可以在加载文件时加载此文件,我创建了一个同名的tmp文件

ajax用于获取文件名列表,生成文件并检查文件存在状态:

getXmlFileName.php

saveTmpData

createTmpFile.php

$.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);
           }
       }
    }
});
  <?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);
?>
   <?php
    header("Content-type: application/json");
   $file_x ="./tmpData/";
   $file_x .= $_POST['name'];
   if (file_exists($file_x))
    {
        echo true;
    }
    else
    {
        echo false;
    }
?>
    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: 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;
        }
    });
},
   <?php
   header("Content-type: application/json");
   $file_x ="./tmpData/";
   $file_x .= $_POST['name'];
   $createFile = touch($file_x);
   echo (file_exists($file_x));
   ?>
$.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;
                       }

                   });