使用PHP将文件从一台服务器移动到另一台服务器的最佳方法是什么?

5 php data-transfer file

我想设置一个运行PHP脚本的CRON,然后将XML文件(保存非敏感信息)从一个服务器移动到另一个服务器.

我已获得正确的用户名/密码,并希望使用SFTP协议.这些工作将每天运行.一台服务器可能是Linux而另一台服务器是Windows.两者都在不同的网络上.

移动该文件的最佳方法是什么?

Ale*_*ian 7

如果两个服务器都在Linux上,您可以将rsync用于任何类型的文件(php,xml,html,binary等).即使其中一个是Windows,也有Windows的rsync端口.


Ste*_*her 7

为什么不尝试使用PHP的FTP功能

然后你可以这样做:

// open some file for reading
$file = 'somefile.txt';
$fp = fopen($file, 'r');

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

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

// try to upload $file
if (ftp_fput($conn_id, $file, $fp, FTP_ASCII)) {
    echo "Successfully uploaded $file\n";
} else {
    echo "There was a problem while uploading $file\n";
}

// close the connection and the file handler
ftp_close($conn_id);
fclose($fp);
Run Code Online (Sandbox Code Playgroud)


wpr*_*prl 5

为什么不使用shell_execscp

<?php
    $output = shell_exec('scp file1.txt dvader@deathstar.com:somedir');
    echo "<pre>$output</pre>";
?>
Run Code Online (Sandbox Code Playgroud)

  • scp 是一个非常方便且功能强大的工具,但可能需要一些配置:http://www.google.com/search?q=+password-less+SSH+login (2认同)