| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 | 
							- <?php
 
- namespace Think\Upload\Driver;
 
- class Ftp {
 
-     
 
-     private $rootPath;
 
-     
 
-     private $error = ''; 
 
-     
 
-     private $link;
 
-     private $config = array(
 
-         'host'     => '', 
 
-         'port'     => 21, 
 
-         'timeout'  => 90, 
 
-         'username' => '', 
 
-         'password' => '', 
 
-     );
 
-     
 
-     public function __construct($config){
 
-         
 
-         $this->config = array_merge($this->config, $config);
 
-         
 
-         if(!$this->login()){
 
-             E($this->error);
 
-         }
 
-     }
 
-     
 
-     public function checkRootPath($rootpath){
 
-         
 
-         $this->rootPath = ftp_pwd($this->link) . '/' . ltrim($rootpath, '/');
 
-         if(!@ftp_chdir($this->link, $this->rootPath)){
 
-             $this->error = '上传根目录不存在!';
 
-             return false;
 
-         }
 
-         return true;
 
-     }
 
-     
 
-     public function checkSavePath($savepath){
 
-         
 
-         if (!$this->mkdir($savepath)) {
 
-             return false;
 
-         } else {
 
-             
 
-             return true;
 
-         }
 
-     }
 
-     
 
-     public function save($file, $replace=true) {
 
-         $filename = $this->rootPath . $file['savepath'] . $file['savename'];
 
-         
 
-         
 
-         
 
-         
 
-         
 
-         
 
-         if (!ftp_put($this->link, $filename, $file['tmp_name'], FTP_BINARY)) {
 
-             $this->error = '文件上传保存错误!';
 
-             return false;
 
-         }
 
-         return true;
 
-     }
 
-     
 
-     public function mkdir($savepath){
 
-         $dir = $this->rootPath . $savepath;
 
-         if(ftp_chdir($this->link, $dir)){
 
-             return true;
 
-         }
 
-         if(ftp_mkdir($this->link, $dir)){
 
-             return true;
 
-         } elseif($this->mkdir(dirname($savepath)) && ftp_mkdir($this->link, $dir)) {
 
-             return true;
 
-         } else {
 
-             $this->error = "目录 {$savepath} 创建失败!";
 
-             return false;
 
-         }
 
-     }
 
-     
 
-     public function getError(){
 
-         return $this->error;
 
-     }
 
-     
 
-     private function login(){
 
-         extract($this->config);
 
-         $this->link = ftp_connect($host, $port, $timeout);
 
-         if($this->link) {
 
-             if (ftp_login($this->link, $username, $password)) {
 
-                return true;
 
-             } else {
 
-                 $this->error = "无法登录到FTP服务器:username - {$username}";
 
-             }
 
-         } else {
 
-             $this->error = "无法连接到FTP服务器:{$host}";
 
-         }
 
-         return false;
 
-     }
 
-     
 
-     public function __destruct() {
 
-         ftp_close($this->link);
 
-     }
 
- }
 
 
  |