简体   繁体   English

使用laravel SSH将文件上传到另一台服务器

[英]Using laravel SSH to upload files to another server

Revised: 修订:

I have an uploaded file that I would like to move to my other server for processing with another application. 我有一个上载的文件,我想移到其他服务器上以使用其他应用程序进行处理。 I thought the best way would be to use SSH built into Laravel however, it's proven to be a headache. 我以为最好的方法是使用Laravel内置的SSH,但是事实证明这很头疼。

In my configuration file (remote.php), I've successfully set up my connection to my other server using SSH keys. 在配置文件(remote.php)中,我已经使用SSH密钥成功建立了到其他服务器的连接。 I tested this by running 我通过运行测试了

SSH::run(['cd /var/www/html', 'ls'], function($line) {

    echo $line . PHP_EOL;    

});

and it gave me a list of files in /var/www/html/ on my remote server. 它给了我远程服务器上/var/www/html/中文件的列表。 All seems good, right? 一切似乎都很好,对吧?

Now I want to use SSH::put() to upload my uploaded file to the remote server. 现在,我想使用SSH::put()将我上传的文件上传到远程服务器。 I used: 我用了:

SSH::put($file->getRealPath(), '/var/www/html/uploads');

Guess what? 你猜怎么了? It ran without an error so I assumed it was uploaded successfully. 它运行没有错误,所以我认为它已成功上传。 When I checked that directory on the remote server, it was blank. 当我在远程服务器上检查该目录时,它是空白的。 I've tried to set file permissions to 777 on /var/www/html/uploads to see if it was a permission issue, same thing--nothing. 我试图在/var/www/html/uploads上将文件权限设置为777,以查看这是否是权限问题,同样,没事。

I also tried using SSH::get() to download a file from my remote server and it ran successfully. 我还尝试使用SSH::get()从远程服务器下载文件,该文件成功运行。

Here's a dump on $file->getRealPath() : 这是$file->getRealPath()上的转储:

string(14) "/tmp/phpikt2mg"

Both machines are running Centos and have their public keys registered on both ends. 两台机器都运行Centos,并在两端注册了其公共密钥。

I'm a little bit late, but just faced the same problem and finally found the reason of failure! 我有点迟了,但只是遇到了同样的问题,终于找到了失败的原因! So it could save some minutes to others. 这样可以节省一些时间。

You should use full path with filename as a second parameter instead of just a path to folder. 您应该使用具有filename的完整路径作为第二个参数,而不仅仅是文件夹的路径。 For this question it should be 对于这个问题应该是

SSH::put($file->getRealPath(), '/var/www/html/uploads/' . $file->getFilename());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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