What is the best way to move files from one server to another with PHP?

孤街浪徒 提交于 2019-11-29 03:54:58

问题


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.

I have been given the proper username/password, and want to use SFTP protocol. The jobs will run daily. There is the potential that one server is Linux and the other is Windows. Both are on different networks.

What is the best way to move that file?


回答1:


If both servers would be on Linux you could use rsync for any kind of files (php, xml, html, binary, etc). Even if one of them will be Windows there are rsync ports to Windows.




回答2:


Why not try using PHP's FTP functions?

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);



回答3:


Why not use shell_exec and scp?

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



回答4:


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)

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

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

This way I have more control on my functions, tests and operations!



来源:https://stackoverflow.com/questions/244752/what-is-the-best-way-to-move-files-from-one-server-to-another-with-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!