Skip to content

Commit db32cee

Browse files
authored
Update Ftp.php
1 parent 91fb6cc commit db32cee

1 file changed

Lines changed: 51 additions & 32 deletions

File tree

src/Ftp.php

Lines changed: 51 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,19 @@ public function __construct($server='',$port='',$username='',$password='',$pasv=
88
$this->port=$config["port"];
99
$this->username=$config["username"];
1010
$this->password=$config["password"];
11-
$this->pasv=$config["pasv"];
11+
$this->pasv=(bool)$config["pasv"];
1212
else:
1313
$this->server=$server;
1414
$this->port=$port;
1515
$this->username=$username;
1616
$this->password=$password;
17-
$this->pasv=$pasv;
17+
$this->pasv=(bool)$pasv;
18+
endif;
19+
$this->ftp=ftp_connect($this->server,$this->port,10) or die("FTP CONNECT ERROR\r\n");
20+
ftp_login($this->ftp,$this->username,$this->password) or die("FTP LOGIN ERROR\r\n");
21+
if(!ftp_pasv($this->ftp,$this->pasv)):
22+
die("无法启用被动模式\r\n");
1823
endif;
19-
$this->ftp=ftp_connect($this->server,$this->port,10) or die("FTP CONNECT ERROR");
20-
ftp_login($this->ftp,$this->username,$this->password) or die("FTP LOGIN ERROR");
21-
ftp_pasv($this->ftp, $this->pasv);
2224
}
2325
/**
2426
* FTP当前位置
@@ -49,14 +51,25 @@ public function RawList($path='/'){
4951
/**
5052
* 当前(本地)上传到FTP
5153
*/
52-
public function Upload($local,$server){
53-
$this->MakeDir(dirname($server));
54-
if (!file_exists($local)){
55-
return false;
56-
}
57-
$result = ftp_put($this->ftp,$server,$local,FTP_BINARY);
58-
return (!$result) ? false : true;
59-
}
54+
public function Upload($local, $server){
55+
$dir = dirname($server);
56+
if(!$this->MakeDir($dir)):
57+
return false;
58+
endif;
59+
$local = realpath($local);
60+
if(!$local || !file_exists($local)):
61+
return false;
62+
endif;
63+
if(!ftp_pasv($this->ftp, $this->pasv)):
64+
return false;
65+
endif;
66+
$result = ftp_put($this->ftp, $server, $local, FTP_BINARY);
67+
if($result):
68+
return true;
69+
else:
70+
return false;
71+
endif;
72+
}
6073
/**
6174
* FTP下载到本地
6275
*/
@@ -90,28 +103,34 @@ public function Del($file) {
90103
/**
91104
* FTP创建目录
92105
*/
93-
public function MakeDir($path){
94-
$path_arr = explode('/',$path);
95-
$file_name = array_pop($path_arr);
96-
$path_div = count($path_arr);
97-
foreach($path_arr as $val){
98-
if(@ftp_chdir($this->ftp,$val) == FALSE){
99-
$tmp = @ftp_mkdir($this->ftp,$val);
100-
if($tmp == FALSE){
101-
throw new \Exception("Dir Error");
102-
exit;
103-
}
104-
@ftp_chdir($this->ftp,$val);
105-
}
106-
}
107-
for($i=1;$i=$path_div;$i++){
108-
@ftp_cdup($this->ftp);
109-
}
110-
}
106+
public function MakeDir($path){
107+
if(empty($path) || $path === '/' || $path === '.'):
108+
return true;
109+
endif;
110+
$dirs = array_filter(explode('/', trim($path, '/')));
111+
if(empty($dirs)):
112+
return true;
113+
endif;
114+
$originalPwd = ftp_pwd($this->ftp);
115+
foreach($dirs as $dir):
116+
if(!@ftp_chdir($this->ftp, $dir)):
117+
if(!@ftp_mkdir($this->ftp, $dir)):
118+
@ftp_chdir($this->ftp, $originalPwd);
119+
return false;
120+
endif;
121+
if (!@ftp_chdir($this->ftp, $dir)):
122+
@ftp_chdir($this->ftp, $originalPwd);
123+
return false;
124+
endif;
125+
endif;
126+
endforeach;
127+
@ftp_chdir($this->ftp, $originalPwd);
128+
return true;
129+
}
111130
/**
112131
* FTP文件大小
113132
*/
114-
public function Size($file) {
133+
public function Size($file){
115134
$res = @ftp_size($this->ftp,$file);
116135
if(!$res):
117136
return false;

0 commit comments

Comments
 (0)