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 be开发者_如何学JAVAtter 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.
精彩评论