-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathftp.php
More file actions
114 lines (80 loc) · 3 KB
/
ftp.php
File metadata and controls
114 lines (80 loc) · 3 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?
/** Object-oriented FTP controller
* Writen by Zdenek Kops in 2015; zdenekkops@gmail.com
* Thanks for inspiration to tendrid@gmail.com at http://php.net/manual/en/book.ftp.php
* Requirements: PHP 5.3+
*
* How to use: call FTP functions without prefix "ftp_" and without first argument
*
* Special methods:
* connect() and ssl_connect() are disabled, use constructor instead
* close() use without any arguments, will close FTP connection and destruct an object
* scandir() is an alias of nlist()
* is_dir() emulates is_dir() function
*
*
* EXAMPLE:
$ftp = new ftp('123.234.214.124');
$ftp->login('user', 'password');
$dir = $ftp->scandir('.'); // filenames are fully addressed, e.g. "directory/file.suffix"
foreach($dir as $file){
$local = $destination.'/'.basename($file);
if($ftp->is_dir($file)){
mkdir($local);
}
else{
$ftp->get($local, $file, FTP_BINARY);
}
}
$ftp->close();
*/
class ftp{
private $connection;
private $connection_type;
private $disabled_functions = ['connect', 'ssl_connect', 'close', 'quit'];
public function __construct($url, $port=21, $timeout=90, $ssl=false){
$url = preg_replace('#^(ftp://|)(.*)$#msi', '$2', $url);
if($ssl){
$ftp = ftp_ssl_connect($url, $port, $timeout);
if($ftp) $this->connection_type = 'SSL-FTP';
else throw new FTP_Exception('FTP: Cannot establish encrypted connection to "'.$url.'".');
}
else{
$ftp = ftp_connect($url, $port, $timeout);
if($ftp) $this->connection_type = 'FTP';
else throw new FTP_Exception('FTP: Cannot establish connection to "'.$url.'".');
}
$this->connection = $ftp;
}
public function close(){
$this->__destruct();
}
public function scandir($path){ // only alias of nlist
return ftp_nlist($this->connection, $path);
}
public function is_dir($path, $slash='/'){ // emulating is_dir() function, $path can be relative
$path = $slash.trim($path, $slash);
$last_slash_pos = mb_strrpos($path, $slash);
$name = mb_substr($path, $last_slash_pos);
$parent = mb_substr($path, 0, $last_slash_pos).'.';
$list = ftp_nlist($this->connection, $parent);
$detailed = ftp_rawlist($this->connection, $parent);
if($list===false or $detailed===false) return 0; // error
elseif($detailed[array_search($name, $list)]{0}==='d') return true; // it is a dir
else return false; // it is not a dir
}
public function __call($function, $arguments){
if(function_exists('ftp_'.$function) and !in_array(strtolower($function), $this->disabled_functions)){
array_unshift($arguments, $this->connection);
return call_user_func_array('ftp_'.$function, $arguments);
}
else throw new FTP_Exception('FTP: Method "'.$function.'" does not exist.');
}
public function __get($name){
return $this->$name;
}
function __destruct(){
@ftp_close($this->connection);
}
}
class FTP_Exception extends Exception{}