-
Notifications
You must be signed in to change notification settings - Fork 4
/
sftp.php
56 lines (44 loc) · 1.68 KB
/
sftp.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
class Sftp {
private $connection;
private $sftp;
public function __construct($host, $port=22) {
$this->connection = ssh2_connect($host, $port);
if (! $this->connection)
throw new \Exception("Could not connect to $host on port $port.");
return $this;
}
static function instance($host, $port=22){
new self($host, $port);
}
public function login($username, $password) {
if (! ssh2_auth_password($this->connection, $username, $password))
throw new \Exception("Could not authenticate with username $username " .
"and password $password.");
$this->sftp = ssh2_sftp($this->connection);
if (! $this->sftp)
throw new \Exception("Could not initialize SFTP subsystem.");
}
public function uploadFile($local_file, $remote_file) {
$sftp = $this->sftp;
$stream = fopen("ssh2.sftp://$sftp$remote_file", 'w');
if (! $stream)
throw new \Exception("Could not open file: $remote_file");
$data_to_send = file_get_contents($local_file);
if ($data_to_send === false)
throw new \Exception("Could not open local file: $local_file.");
if (fwrite($stream, $data_to_send) === false)
throw new \Exception("Could not send data from file: $local_file.");
fclose($stream);
}
public function iteratorFile($remote_file) {
$sftp = $this->sftp;
$stream = fopen("ssh2.sftp://$sftp$remote_file", 'r');
if (! $stream)
throw new \Exception("Could not open file: $remote_file");
while($line = fgets($stream) ){
yield $line;
}
fclose($stream);
}
}