FTP上傳類php
1 <?php 2 3 namespace ftp; 4 5 /*$config = array( 6 'host' => '127.0.0.1', //服務器 7 'port' => 21, //端口 8 'timeout' => 90, //超時時間 9 'username' => 'test', //用戶名 10 'password' => 'test', //密碼 11 'is_pasv' => true, 12 'domain' => 'img.test.com' , 13 ); 14 15 try { 16 $ftp = new Ftp($config); 17 $ftp->checkRootPath("/demo"); 18 $ftp->checkSavePath("abc"); 19 $ftp->upload('formfilename'); 20 var_dump($ftp->get_upload_info()); 21 } catch (FtpException $e) { 22 echo $e->getMessage(); 23 echo $e->getCode(); 24 }*/ 25 26 /** 27 * 錯誤異常異常代碼編號 28 * 21000,連接登陸FTP失敗 29 * 21001,上傳根目錄不存在 30 * 21002,目錄建立失敗 31 * 21003,文件後綴不可上傳 32 * 21004,上傳失敗 33 */ 34 class FtpException 35 { 36 } 37 38 class Ftp 39 { 40 /** 41 * 上傳文件根目錄 42 * @var string 43 */ 44 private $rootPath; 45 46 private $savepath = ''; 47 private $filename = ''; 48 49 /** 50 * FTP鏈接 51 * @var resource 52 */ 53 private $link; 54 // 文件命名規則 (md5,timestamp,sha1,time); 55 private $config = array( 56 'host' => '', //服務器 57 'port' => 21, //端口 58 'timeout' => 60, //超時時間 59 'username' => '', //用戶名 60 'password' => '', //密碼 61 'is_pasv' => false,//開啓被動模式 62 'filename' => 'md5', 63 'allow_file_extension' => array('gif', 'png', 'jpg', 'jpeg', 'doc', 'excel'), 64 'domain' => 'img.test.com', 65 ); 66 67 /** 68 * 構造函數,用於設置上傳根路徑 69 * @param array $config FTP配置 70 */ 71 public function __construct($config) 72 { 73 /* 默認FTP配置 */ 74 $this->config = array_merge($this->config, $config); 75 76 /* 登陸FTP服務器 */ 77 if (!$this->login()) { 78 throw new FtpException($this->error, 21000); 79 } 80 } 81 82 /** 83 * 檢測上傳根目錄 84 * @param string $rootpath 根目錄 85 * @return boolean true-檢測經過,false-檢測失敗,拋出異常 86 */ 87 public function checkRootPath($rootpath) 88 { 89 /* 設置根目錄 */ 90 $this->rootPath = @ftp_pwd($this->link) . ltrim($rootpath, '/'); 91 92 if (!@ftp_chdir($this->link, $this->rootPath)) { 93 throw new FtpException('上傳根目錄不存在!(' . $this->rootPath . ')', 21001); 94 } 95 return true; 96 } 97 98 /** 99 * 設置上傳目錄 100 * @param string $savepath 上傳目錄 101 * @return boolean 檢測結果,true-經過,false-失敗 102 */ 103 public function checkSavePath($savepath) 104 { 105 $this->savepath = $savepath = "/" . ltrim($savepath, '/'); 106 /* 檢測並建立目錄 */ 107 if (!$this->mkdir($savepath)) { 108 return false; 109 } else { 110 //TODO:檢測目錄是否可寫 111 return true; 112 } 113 } 114 115 /** 116 * 保存指定文件 117 * @param array $file 保存的文件信息 118 * @param String $user_defined_filename 自定義文件名 119 * @return boolean 保存狀態,true-成功,false-失敗 120 */ 121 public function upload($file, $user_defined_filename = '') 122 { 123 if (empty($user_defined_filename)) { 124 $filename = $this->filenamerule(); 125 } else { 126 $filename = $user_defined_filename; 127 } 128 $uploadfile = $_FILES[$file]; 129 $file_ext = $this->_get_file_extension($uploadfile['name']); 130 $this->filename = $filename . "." . $file_ext; 131 $fileurl = $this->rootPath . $this->savepath . "/" . $this->filename; 132 /* 移動文件 */ 133 if (!ftp_put($this->link, $fileurl, $uploadfile['tmp_name'], FTP_BINARY)) { 134 throw new FtpException("{$filename}.{$file_ext} 上傳失敗", 21004); 135 } 136 return true; 137 } 138 139 public function get_upload_info() 140 { 141 @extract($this->config); 142 $result['domain_all_path'] = "//" . $domain . $this->rootPath . $this->savepath . "/" . $this->filename; 143 $result['all_path'] = $this->rootPath . $this->savepath . "/" . $this->filename; 144 $result['save_path_file'] = $this->savepath . "/" . $this->filename; 145 return $result; 146 } 147 148 private function filenamerule() 149 { 150 @extract($this->config); 151 switch ($filename) { 152 case 'sha1': 153 $filename = sha1(microtime()); 154 break; 155 case 'md5': 156 $filename = md5(microtime()); 157 break; 158 case 'time': 159 $filename = date('YmdHis'); 160 break; 161 case 'timestamp': 162 $filename = time(); 163 break; 164 } 165 return $filename; 166 } 167 168 private function _get_file_extension($file) 169 { 170 @extract($this->config); 171 $file_ext = pathinfo($file, PATHINFO_EXTENSION); 172 if (in_array($file_ext, $allow_file_extension)) { 173 return $file_ext; 174 } else { 175 throw new FtpException(" {$file_ext} 不可上傳", 21003); 176 } 177 178 } 179 180 /** 181 * 建立目錄 182 * @param string $savepath 要建立的目錄 183 * @return boolean 建立狀態,true-成功,false-失敗 184 */ 185 public function mkdir($savepath) 186 { 187 $dir = $this->rootPath . $savepath; 188 if (@ftp_chdir($this->link, $dir)) { 189 return true; 190 } 191 192 if (@ftp_mkdir($this->link, $dir)) { 193 return true; 194 } elseif ($this->mkdir(dirname($savepath)) && @ftp_mkdir($this->link, $dir)) { 195 return true; 196 } else { 197 throw new FtpException("目錄 {$savepath} 建立失敗,可能設置寫入權限和權限組不對!", 21002); 198 } 199 } 200 201 /** 202 * 登陸到FTP服務器 203 * @return boolean true-登陸成功,false-登陸失敗 204 */ 205 private function login() 206 { 207 @extract($this->config); 208 $this->link = @ftp_connect($host, $port, $timeout); 209 if ($this->link) { 210 if (@ftp_login($this->link, $username, $password)) { 211 if ($is_pasv) { 212 @ftp_pasv($this->link, true); 213 } 214 return true; 215 } else { 216 $this->error = "沒法登陸到FTP服務器:username - {$username}"; 217 } 218 } else { 219 $this->error = "沒法鏈接到FTP服務器:{$host}"; 220 } 221 return false; 222 } 223 224 /** 225 * 析構方法,用於斷開當前FTP鏈接 226 */ 227 public function __destruct() 228 { 229 @ftp_close($this->link); 230 } 231 232 }