-
Notifications
You must be signed in to change notification settings - Fork 0
/
ftp.php
154 lines (125 loc) · 3.16 KB
/
ftp.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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
/**
* Created by IntelliJ IDEA.
* User: reddeath
* Date: 1/11/2018
* Time: 3:04 PM
*/
class pftp
{
public $conn;
private $server;
public function __construct($_server,$_user,$_pass)
{
if(!$this->connect($_server,$_user,$_pass)){
die('Connection failed to the server:' .$this->server);
}
}
/**
* make connection to the server
* @return bool
*/
private function connect($_server,$_user,$_pass){
$connected = false;
$login = false;
set_time_limit(0);
define('FTP_SERVER',$_server);
define('FTP_USER',$_user);
define('FTP_PASSWORD',$_pass);
define('FTP_PASSIVE',FALSE);
define('FTP_PORT',21);
define('FTP_TIME_OUT',90);
/**
* setup basic connection
*/
$this->conn = ftp_connect(FTP_SERVER,FTP_PORT,FTP_TIME_OUT);
$this->server = FTP_SERVER;
if($this->conn){
/**
* login to the ftp server
*/
$login = ftp_login($this->conn,FTP_USER,FTP_PASSWORD);
/**
* Set passve mode ON/OFF (default OFF)
*/
ftp_pasv($this->conn,FTP_PASSIVE);
}
/**
* Check connection
*/
if($this->conn && $login){
$connected = true;
}
return $connected;
}
/**
* Make ftp directory
* @param $dir
* @return bool
*/
public function mkdir($dir){
return @ftp_mkdir($this->conn,$dir);
}
/**
* @param $file
* @param $newfile
* @return bool
*/
public function upload($file,$newfile){
/**
* Transfer mode
*/
$ascii = array('txt',"cvs");
$ext = end(explode(".",$ascii));
$mode = FTP_BINARY;
if(in_array($ext, $ascii, true)){
$mode = FTP_ASCII;
}
return @ftp_put($this->conn,$newfile,$file,$mode);
}
/**
* @param $dir
* @return bool
*/
public function chdir($dir){
return @ftp_chdir($this->conn, $dir);
}
/**
* @param string $dir
* @param string $params
* @return array|bool
*/
public function drl($dir = '.',$params = '-la'){
$data = @ftp_nlist($this->conn,$params. ' '. $dir);
if(count($data) < 1){
$data = false;
}
return $data;
}
/**
* Download the file from the server
* @param $file
* @param $newfile
* @return bool
*/
public function download($file,$newfile){
/**
* Transfer mode
*/
$ascii = array('txt', 'cvs');
$ext = end(explode('.',$ascii));
$mode = FTP_BINARY;
if(in_array($ext, $ascii, true)){
$mode = FTP_ASCII;
}
return @ftp_get($this->conn,$newfile,$file,$mode);
}
/**
* Close connection
*/
public function __destruct()
{
@ftp_quit($this->conn);
@ftp_close($this->conn);
}
}