本地文件批量上傳到FTP服務器

/*******
	本地文件批量上傳到FTP服務器
*******/
class Ftp_Class
	{
		private $username=null;
		private $pwd=null;
		private $connection=null;
		private $url=null;
		//禁止的文件上傳類型
		private $type=array(
							'txt',
                            'text',
                            'php',
                            'phps',
                            'php4',
                            'js',
                            'css',
                            'htm',
                            'html',
                            'phtml',
                            'shtml',
                            'log',
                            'xml',
							'exe'
							);
		//允許上傳的文件類型
		private $allowtype=array(
									'jpg',
									'jpeg',
									'gif',
									'png',
									'bmp'
								);						
		
		public function __construct()
			{
				
			}
		
		private set_username($name)
			{
				$this->username=$name;
			}
			
		private set_pwd($pwd)
			{
				$this->pwd;
			}
			
		//登陸FTP服務器實例	
		public get_instance($username,$pwd,$url)
			{
				//$this->set_params();
				$this->username=$username;
				$this->pwd=$pwd;
				$this->url=$url;
				
				if($this->username && $this->pwd && $this->url)
					{
						$this->connection=ftp_connect($this->url);
						ftp_login($this->connection,$this->username,$this->pwd);
						return true;
					}
					else
						{
							return false;
						}
			}
			
		//上傳文件
		private function putfile($file,$newfile)
			{
				$code=mb_detect_encoding($newfile);
				if($code!='utf-8')
					{
						$newfile=iconv($code,'utf-8',$newfile);
					}
					
				ftp_put($this->connection,$newfile,$file,FTP_BINARY);
			}
			
		//下載文件
		private function getfile($file,$newfile)
			{
				ftp_get($this->connection,$newfile,$file,FTP_BINARY);
			}
			
		//取得遠程文件夾列表
		private function dirlist($dir=".")
			{
				if(!$this->connection)
					{
						return false;
					}
				$list=ftp_nlist($this->connection,$dir);
				return $list;
			}
			
		private function ftppwd()
			{
				return ftp_pwd($this->connection);
			}
			
		//退出FTP
		public function ftpclose()
			{
				ftp_close($this->connection);
			}
		
		//檢查上傳文件是否合法
		private function check_type($types)
			{
				if(!$types)
					{
						return false;
					}
					
				if(in_array($types,$this->allowtype))
					{
						return false;
					}
					
				return true;
				
			}
			
		//$newpath=‘文件要上傳到的目錄’	
		private function domulti_mkdir($path,$newpath)
			{
				if(!is_dir($path) && !is_dir($newpath))
					{
						return false;
					}
					
				$handle=opendir($path);
				
				while(false!==($file=readdir($handle)))
					{
						if($file!='.' && $file!='..')
							{
								if(is_dir($path.'/'.$file))
									{
										$paths=scandir($path.'/'.$file);
										
										/* if(!is_dir($newpath.'/'.$file))
											{
												$this->mkdirs($newpath.'/'.$file);
											} */
											
										if(!$this->ftpchdir($newpath.'/'.$file))
											{
												if($this->ftpchdir($newpath))
													{
														$new_file=$newpath.'/'.$file;
														/* $newcode=mb_detect_encoding($new_file);
														if($newcode!='utf-8')
															{
																$new_file=iconv($newcode,'utf-8',$new_file);
															} */
														$this->mkdirs($newpath.'/'.$file);
													}
												//$this->mkdirs($newpath.'/'.$file);
											}	
											
										$this->multi_mkdir($paths,$path.'/'.$file,$newpath.'/'.$file);
									}
							}
					}

			}
			
		//遞歸生成文件夾且移動文件
		private function multi_mkdir($path,$parent,$newpath)
			{
				if(!is_array($path) && !$parent && !$newpath)
					{
						return false;
					}
				
				foreach($path as $key=>$value)
								{
									
									if($value!='.' && $value!='..')
										{
									
											if(is_dir($parent.'/'.$value))
												{
													$parents= $parent.'/'.$value;
													$newpaths=$newpath.'/'.$value;
													
													if(!$this->ftpchdir($newpaths))
														{
															if($this->ftpchdir($newpath))
																{
																	$this->mkdirs($newpaths);
																}
															//$this->mkdirs($newpaths);
														}
														
													/* if(!is_dir($newpaths))
														{
															$this->mkdirs($newpaths);
														} */
														
														
													if(is_dir($parents))
														{
															$paths=scandir($parents);
															$this->multi_mkdir($paths,$parents,$newpaths);
														}
													
												}
												elseif(is_file($parent.'/'.$value))
													{
														$filename=$parent.'/'.$value;
														$ext=substr($value,strrpos($value,'.'));
														
														if($this->check_type($ext))
															{
																//執行上傳文件
															}
															
														
														
													}
										}
										else
											{
												
											}
								}
			}
			
		//創建FTP文件夾	
		private function mkdirs($path)
			{
				$code=mb_detect_encoding($path);
				if($code!='utf-8')
					{
						$path=iconv($code,'utf-8',$path);
					}
				
				if(@ftp_chdir($this->connection,$path)==false)
					{
						$flag=ftp_mkdir($this->connection,$path);
						if(!$flag)
							{
								return false;
							}
					}
				//ftp_chdir($this->connection,$path);
				return true;
			}
			
		//文件指針跳轉到指定文件夾下	
		private function ftpchdir($path)
			{
				$code=mb_detect_encoding($path);
				if($code!='utf-8')
					{
						$path=iconv($code,'utf-8',$path);
					}
					
				if(!$path)
					{
						return false;
					}
				ftp_chdir($this->connection,$path);
			}
			
		//對外執行接口	
		public function display($path,$newpath)
			{
				$this->domulti_mkdir($path,$newpath);
			}
			
		
	}
	
	header('content-type:text/html;charset=utf-8');
	$name='';
	$pwd='';
	$url='';
	$path='';
	$newpath='';
	
	$ftp=new Ftp_Class();
	$stance=$ftp->get_instance($name,$pwd,$url);
	if($stance)
		{
			$ftp->display($path,$newpath);
		}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章