Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过ftp将本地主机与服务器同步_Ftp_Localhost_Sync_Remote Server - Fatal编程技术网

通过ftp将本地主机与服务器同步

通过ftp将本地主机与服务器同步,ftp,localhost,sync,remote-server,Ftp,Localhost,Sync,Remote Server,我想实时自动地将本地主机(Windows)与远程服务器同步。所以,当我修改、创建或删除一个文件时,这个工具应该会自动更新远程服务器。此应用程序必须确保两台服务器实时同步。求你了,我真的需要你的帮助。我试过FTPbox,但它不总是更新,我需要一些更好的。我在windows上工作,但如果存在,linux上的一些更好 谢谢如果您不需要同步太多信息,请尝试。我假设您希望同步数据库和文件。这是我的出路,我希望它能对别人有所帮助 第一个代码是本地代码,另一个是远程代码 //make sure you are

我想实时自动地将本地主机(Windows)与远程服务器同步。所以,当我修改、创建或删除一个文件时,这个工具应该会自动更新远程服务器。此应用程序必须确保两台服务器实时同步。求你了,我真的需要你的帮助。我试过FTPbox,但它不总是更新,我需要一些更好的。我在windows上工作,但如果存在,linux上的一些更好


谢谢

如果您不需要同步太多信息,请尝试。

我假设您希望同步数据库和文件。这是我的出路,我希望它能对别人有所帮助

第一个代码是本地代码,另一个是远程代码

//make sure you are connected to your local database

<?php

//function to check internet connection.

function is_connected() {
  if($connected = fsockopen("www.example.com", 80)){
  // website, port  (try 80 or 443)
  if ($connected){
    $is_conn = true; //action when connected
    fclose($connected);
  }

return $is_conn;

} else {
    $is_conn = false; //action in connection failure
  }
}

//if connected to internet, do the following...
if(is_connected()== true){

  echo "connected"; 

  ini_set('max_execution_time', 3000);//increase this incase of slow internet

$table_name = TableName::find_all();
//whatever way you find an array
//of all your entries on this particular 
//table that you want to sync with the remote table.

$file = 'to_upload_local.php'; //a local file to put table contents into
$current = serialize($table_name);//serialize the table contents (google).
file_put_contents($file, $current);//put the serialized contents to the file.

$remote_file = 'public_html/to_upload_remote.php';//this is the file that is on the remote server that you want to overwrite with the local file to upload. 

$ftp_server = "ftp.yourwebsite.org";// your ftp address

// set up basic connection
$conn_id = ftp_connect($ftp_server);

// login with username and password
$login_result = ftp_login($conn_id, "yourFTPUsername", "yourFTPPassword");

// turn passive mode on
ftp_pasv($conn_id, true);

// upload a file
if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)){
    echo "Upload Successful";
} else {

}

// close the connection
ftp_close($conn_id);

//this script called below is to update your remote database. Its in the next example

$call_script = file_get_contents('http://path_to_your_script');
} else { 
 //if not connected to internet,....
echo "offline";
}
?>
//确保已连接到本地数据库
应该完成这项工作的在线脚本(您在前面代码的最后一行中调用的脚本)应该如下所示:

//make sure you're connected to remote database
<?php 

//this function should compare num_rows of your two 
//databases values (local  remote) and return the 
//difference. It's used with array_udiff function.
function compare_objects($obj_a, $obj_b) {
  return $obj_a->id - $obj_b->id;
}
//this function should compare contents of your two 
//databases values (local  remote) and return the 
//difference. It's used with array_udiff function.
function comparison($obj_a, $obj_b){
   if ($obj_a==$obj_b){
     return 0;
   }else{
    return -1;
   }
 }


$file = '../to_upload_remote.php';//the uploaded file
$current = file_get_contents($file);//load the file
$array = unserialize($current);//unserialize to get the object array
$remote_table_name = remote_table_name::find_all();//get what you have in 
//remote database

//if a new value is added, create a new entry to database with new vals
if($try_new = array_udiff($array, $remote_table_name, 'compare_objects')){
  foreach($try_new as $entry){
  $remote_table_name = new remote_table_name();
  $remote_table_name->value = $entry->value;
  //depending on the number of your columns,
  //add values to remote table that were not there before.
  //you can use any other suitable method to do this.
      if($remote_table_name->save()){
          echo "the remote_table_name was saved successfully";
      }
  }
} else {
  echo "same number of rows";
}

//if some values are changed, update them with new vals
if($try_change = array_udiff($array, $remote_table_name, 'comparison')){
  foreach($try_change as $entry){
  $remote_table_name = remote_table_name::find_by_id($entry->id);
  $remote_table_name->value = $entry->value;
    //depending on the number of your columns,
    //update values to remote table.
    //you can use any other suitable method to do this.
    if($remote_table_name->save()){
        echo "the remote_table_name was saved successfully";
    }
  }
} else {
  echo "All values match";
}

?>
//确保已连接到远程数据库
因此,无论何时执行第一个代码,它都会读取本地表,获取所有值并将其放入本地文件,上载本地文件并替换远程文件夹中的一个,调用远程脚本检查未序列化的本地表,并将其与联机表进行比较,然后做必要的事情。

有一个可以做你想做的事情


对于linux用户,您可以查看一下。

如果您要执行自动同步,您还将自动部署bug。。。对windows不太清楚-但您可以在linux中使用它来安排运行,比如说,每分钟运行一次。您可以将它与执行以下操作的shell脚本一起使用