开发者

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

开发者 https://www.devze.com 2022-12-19 07:45 出处:网络
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 accomplis

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号