[英]What is the best way to move files from one server to another with PHP?
I want to setup a CRON that runs a PHP script that in turn moves XML file (holding non-sensitive information) from one server to another. 我想设置一个运行PHP脚本的CRON,然后将XML文件(保存非敏感信息)从一个服务器移动到另一个服务器。
I have been given the proper username/password, and want to use SFTP protocol. 我已获得正确的用户名/密码,并希望使用SFTP协议。 The jobs will run daily.
这些工作将每天运行。 There is the potential that one server is Linux and the other is Windows.
一台服务器可能是Linux而另一台服务器是Windows。 Both are on different networks.
两者都在不同的网络上。
What is the best way to move that file? 移动该文件的最佳方法是什么?
Why not try using PHP's FTP functions ? 为什么不尝试使用PHP的FTP功能 ?
Then you could do something like: 然后你可以这样做:
// 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);
Why not use shell_exec and scp ? 为什么不使用shell_exec和scp ?
<?php
$output = shell_exec('scp file1.txt dvader@deathstar.com:somedir');
echo "<pre>$output</pre>";
?>
I had some similar situation. 我有类似的情况。 After some tries, I did some thing different
经过一些尝试,我做了一些不同的事情
We have 2 servers, a (that have the original files) b (files should moved to it) 我们有2台服务器,一台(有原始文件)b(文件应移到它)
And for sure the data is NOT sensitive 并且肯定数据不敏感
Now in server a I made a file to do the following when called: 1. Choose the file to move 2. Zip the file 3. Print the .zip file location 4. Delete the .zip file (and the original file) if delete parameter passes 现在在服务器a中我调用了一个文件来执行以下操作:1。选择要移动的文件2.压缩文件3.打印.zip文件位置4.如果删除则删除.zip文件(和原始文件)参数传递
In the server b the file should do: 1. Call the file on the a server 2. Download the zip file 3. Unzip and copy it to the proper location 4. Call the delete function on server a 在服务器b中,文件应该:1。调用服务器上的文件2.下载zip文件3.解压缩并将其复制到正确的位置4.调用服务器上的删除功能a
This way I have more control on my functions, tests and operations! 通过这种方式,我可以更好地控制我的功能,测试和操作!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.