简体   繁体   中英

What method is the best to copy files from one server to another using PHP

I need to copy a couple of files over from one server to another using PHP. Now I currently use PHP FTP functionality which is ok but causes issues. Are there better ways to accomplish this?

FTP is a good way to do this. It handles the authentication, and there are native PHP functions to use with FTP.

I would suggest rsync , or sftp

However, if restricted to PHP only, try ssh2_sftp

required - pecl ssh2 package

with the proper configured ssh public key, you don't even require to supply password

Your best bet is usually rsync , especially if you need to copy a directory structure. It's not strictly a PHP method, but you can call using shell_exec() or backtick syntax:

`rsync -acz /files/to/copy/ user@remotehost:/target/dir/`

You'll need to use public key authentication, but there are plenty of tutorials around on that. If you know the location of a public key file that will get you into the remote server, you can use the -e switch:

`rsync -acze 'ssh -i /path/to/your.key' /files...`

Check out the rsync website for more info.

PHP's backtick syntax works with variable interpolation , so you can use variables like $source , $target etc in your syntax.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM