简体   繁体   English

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

[英]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? 移动该文件的最佳方法是什么?

If both servers would be on Linux you could use rsync for any kind of files (php, xml, html, binary, etc). 如果两个服务器都在Linux上,您可以将rsync用于任何类型的文件(php,xml,html,binary等)。 Even if one of them will be Windows there are rsync ports to Windows. 即使其中一个是Windows,也有Windows的rsync端口。

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_execscp

<?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.

相关问题 使用PHP将文件从一台服务器复制到另一台服务器最好的方法是什么 - What method is the best to copy files from one server to another using PHP 如何通过一些简短的方式将所有文件和文件夹从一个文件夹移动到另一个 php 中的 S3 存储桶? - How to move all files and folder from one folder to another of S3 bucket in php with some short way? 在 AWS S3 中将多个文件从一个存储桶复制到另一个存储桶的最佳方法是什么? - What is the best way to copy multiple files from one bucket to another in AWS S3? 服务器端验证显示空白字段,并且是将消息从一页显示到另一页的最佳方法是什么? - Server-side validation showing a blank field and what is the best way to display message from one page to another? 将PHP文件从一台服务器复制到另一台服务器 - Copying PHP files from one server to another PHP中将一个多维数组追加到另一个多维数组的最佳方法是什么? - What is the best way in PHP to append one multidimensional array to another? PHP:从服务器到服务器传输文件的最佳方法 - PHP : Best way to transfer files from server to server 在PHP中移动小数点的最佳方法是什么 - What is the best way to move decimal point in PHP PHP脚本将目录从一台Linux服务器移至另一台? - PHP script to move a directory from one Linux server to another? 将数据从一个CLI应用程序传递到另一个的最佳方法是什么 - What is the best way to pass data from one cli app to another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM