File tree Expand file tree Collapse file tree 3 files changed +56
-1
lines changed Expand file tree Collapse file tree 3 files changed +56
-1
lines changed Original file line number Diff line number Diff line change @@ -62,6 +62,28 @@ private function sanityCheck()
6262 }
6363 }
6464
65+ public function upload (string $ localPath , string $ remotePath ): bool
66+ {
67+ if (!is_resource ($ this ->resource )) {
68+ throw new InvalidArgumentException ('The session resource is invalid. ' );
69+ }
70+
71+ if (!file_exists ($ localPath )) {
72+ throw new InvalidArgumentException ('The localPath is invalid. ' );
73+ }
74+
75+ return ssh2_scp_send ($ this ->resource , $ localPath , $ remotePath );
76+ }
77+
78+ public function download (string $ remotePath , string $ localPath ): bool
79+ {
80+ if (!is_resource ($ this ->resource )) {
81+ throw new InvalidArgumentException ('The session resource is invalid. ' );
82+ }
83+
84+ return ssh2_scp_recv ($ this ->resource , $ remotePath , $ localPath );
85+ }
86+
6587 public function connect (): self
6688 {
6789 $ this ->sanityCheck ();
@@ -104,4 +126,4 @@ public function run(string $command)
104126
105127 return new SSHCommand ($ this ->resource , $ command );
106128 }
107- }
129+ }
Original file line number Diff line number Diff line change 55
66final class SSHConnectionTest extends TestCase
77{
8+ public function testUpload ()
9+ {
10+ $ connection = (new SSHConnection ())
11+ ->to ('localhost ' )
12+ ->onPort (22 )
13+ ->as ('travis ' )
14+ ->withKeyPair ('/home/travis/.ssh/id_rsa.pub ' , '/home/travis/.ssh/id_rsa ' )
15+ ->connect ();
16+
17+ $ remotePath = __DIR__ . '/../fixtures/upload.txt ' ;
18+ $ localPath = __DIR__ . '/../fixtures/file.txt ' ;
19+
20+ $ this ->assertTrue ($ connection ->upload ($ localPath , $ remotePath ));
21+ $ this ->assertFileExists ($ remotePath );
22+ }
23+
24+ public function testDownload ()
25+ {
26+ $ connection = (new SSHConnection ())
27+ ->to ('localhost ' )
28+ ->onPort (22 )
29+ ->as ('travis ' )
30+ ->withKeyPair ('/home/travis/.ssh/id_rsa.pub ' , '/home/travis/.ssh/id_rsa ' )
31+ ->connect ();
32+
33+ $ remotePath = __DIR__ . '/../fixtures/file.txt ' ;
34+ $ localPath = __DIR__ . '/../fixtures/download.txt ' ;
35+
36+ $ this ->assertTrue ($ connection ->download ($ remotePath , $ localPath ));
37+ $ this ->assertFileExists ($ localPath );
38+ }
39+
840 public function testSSHConnectionWithKeyPair ()
941 {
1042 $ connection = (new SSHConnection ())
Original file line number Diff line number Diff line change 1+ test.upload.download
You can’t perform that action at this time.
0 commit comments