| 1 |
<?php |
| 2 |
/* from http://www.solutionbot.com/2009/01/02/php-ftp-class/ */ |
| 3 |
class ftp_wrapper |
| 4 |
{ |
| 5 |
private $conn_id; |
| 6 |
private $host; |
| 7 |
private $username; |
| 8 |
private $password; |
| 9 |
private $port; |
| 10 |
public $timeout = 90; |
| 11 |
public $passive = false; |
| 12 |
public $ssl = false; |
| 13 |
public $system_type = ''; |
| 14 |
|
| 15 |
public function __construct($host, $username, $password, $port = 21) |
| 16 |
{ |
| 17 |
$this->host = $host; |
| 18 |
$this->username = $username; |
| 19 |
$this->password = $password; |
| 20 |
$this->port = $port; |
| 21 |
} |
| 22 |
|
| 23 |
public function connect() |
| 24 |
{ |
| 25 |
if ($this->ssl == false) |
| 26 |
{ |
| 27 |
$this->conn_id = ftp_connect($this->host, $this->port); |
| 28 |
} |
| 29 |
else |
| 30 |
{ |
| 31 |
if (function_exists('ftp_ssl_connect')) |
| 32 |
{ |
| 33 |
$this->conn_id = ftp_ssl_connect($this->host, $this->port); |
| 34 |
} |
| 35 |
else |
| 36 |
{ |
| 37 |
return false; |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
if ($this->conn_id === false) return false; |
| 42 |
|
| 43 |
$result = ftp_login($this->conn_id, $this->username, $this->password); |
| 44 |
|
| 45 |
if ($result == true) |
| 46 |
{ |
| 47 |
ftp_set_option($this->conn_id, FTP_TIMEOUT_SEC, $this->timeout); |
| 48 |
|
| 49 |
if ($this->passive == true) |
| 50 |
{ |
| 51 |
ftp_pasv($this->conn_id, true); |
| 52 |
} |
| 53 |
else |
| 54 |
{ |
| 55 |
ftp_pasv($this->conn_id, false); |
| 56 |
} |
| 57 |
|
| 58 |
$this->system_type = ftp_systype($this->conn_id); |
| 59 |
|
| 60 |
return true; |
| 61 |
} |
| 62 |
else |
| 63 |
{ |
| 64 |
return false; |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
public function put($local_file_path, $remote_file_path, $mode = FTP_ASCII) |
| 69 |
{ |
| 70 |
if (ftp_put($this->conn_id, $remote_file_path, $local_file_path, $mode)) |
| 71 |
{ |
| 72 |
return true; |
| 73 |
} |
| 74 |
else |
| 75 |
{ |
| 76 |
return false; |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
public function get($local_file_path, $remote_file_path, $mode = FTP_ASCII) |
| 81 |
{ |
| 82 |
if (ftp_get($this->conn_id, $local_file_path, $remote_file_path, $mode)) |
| 83 |
{ |
| 84 |
return true; |
| 85 |
} |
| 86 |
else |
| 87 |
{ |
| 88 |
return false; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
public function chmod($permissions, $remote_filename) |
| 93 |
{ |
| 94 |
if ($this->is_octal($permissions)) |
| 95 |
{ |
| 96 |
$result = ftp_chmod($this->conn_id, $permissions, $remote_filename); |
| 97 |
if ($result) |
| 98 |
{ |
| 99 |
return true; |
| 100 |
} |
| 101 |
else |
| 102 |
{ |
| 103 |
return false; |
| 104 |
} |
| 105 |
} |
| 106 |
else |
| 107 |
{ |
| 108 |
throw new Exception('$permissions must be an octal number'); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
public function chdir($directory) |
| 113 |
{ |
| 114 |
ftp_chdir($this->conn_id, $directory); |
| 115 |
} |
| 116 |
|
| 117 |
public function delete($remote_file_path) |
| 118 |
{ |
| 119 |
if (ftp_delete($this->conn_id, $remote_file_path)) |
| 120 |
{ |
| 121 |
return true; |
| 122 |
} |
| 123 |
else |
| 124 |
{ |
| 125 |
return false; |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
public function make_dir($directory) |
| 130 |
{ |
| 131 |
if (ftp_mkdir($this->conn_id, $directory)) |
| 132 |
{ |
| 133 |
return true; |
| 134 |
} |
| 135 |
else |
| 136 |
{ |
| 137 |
return false; |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
public function rename($old_name, $new_name) |
| 142 |
{ |
| 143 |
if (ftp_rename($this->conn_id, $old_name, $new_name)) |
| 144 |
{ |
| 145 |
return true; |
| 146 |
} |
| 147 |
else |
| 148 |
{ |
| 149 |
return false; |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
public function remove_dir($directory) |
| 154 |
{ |
| 155 |
if (ftp_rmdir($this->conn_id, $directory)) |
| 156 |
{ |
| 157 |
return true; |
| 158 |
} |
| 159 |
else |
| 160 |
{ |
| 161 |
return false; |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
public function dir_list($directory) |
| 166 |
{ |
| 167 |
$contents = ftp_nlist($this->conn_id, $directory); |
| 168 |
return $contents; |
| 169 |
} |
| 170 |
|
| 171 |
public function cdup() |
| 172 |
{ |
| 173 |
ftp_cdup($this->conn_id); |
| 174 |
} |
| 175 |
|
| 176 |
public function current_dir() |
| 177 |
{ |
| 178 |
return ftp_pwd($this->conn_id); |
| 179 |
} |
| 180 |
|
| 181 |
private function is_octal($i) |
| 182 |
{ |
| 183 |
return decoct(octdec($i)) == $i; |
| 184 |
} |
| 185 |
|
| 186 |
public function __destruct() |
| 187 |
{ |
| 188 |
if ($this->conn_id) |
| 189 |
{ |
| 190 |
ftp_close($this->conn_id); |
| 191 |
} |
| 192 |
} |
| 193 |
} |
| 194 |
?> |