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

谁说胖子不能爱 提交于 2019-11-30 05:28:06

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.

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

Why not use shell_exec and 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)

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!

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