php ftp类


  1. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />  
  2. <?php  
  3. /* 
  4. php ftp类主要功能: 
  5. 1.连接ftp并登陆; 
  6. 2.创建目录和删除目录; 
  7. 3.上传文件和删除文件; 
  8. */  
  9.   
  10.    
  11.   
  12. include 'config.php';  
  13.   
  14. class Net_FTP {  
  15. var $ftp_server;  
  16. var $ftp_user;  
  17. var $ftp_pass;  
  18. var $ftp_port;  
  19. var $conn_id;  
  20. function Net_FTP() {  
  21.     $this->ftp_server = server;  
  22.     $this->ftp_user = username;  
  23.     $this->ftp_pass = password;  
  24.     $this->ftp_port = port;  
  25.     // 建立连接  
  26.     $this->conn_id = ftp_connect($this->ftp_server, $this->ftp_port) or die("不能够连接到 $this->ftp_server");  
  27.        // 尝试登陆  
  28.        if (!ftp_login($this->conn_id, $this->ftp_user, $this->ftp_pass))   
  29.         {  
  30.             $this->message("连接失败 $this->ftp_user");  
  31.         }  
  32.         else  
  33.         {  
  34.             $this->message("连接成功 $this->ftp_user ");  
  35.         }  
  36. }  
  37. //功能:创建新的目录  
  38. //$path默认为空目录  
  39. //创建成功返回true,否则返回false。  
  40. function newdir($path = null)  
  41. {  
  42.         if($this->ftp_is_dir($this->conn_id,$path)||@ftp_mkdir($this->conn_id,$path))  
  43.             return true;  
  44.         if(!$this->newdir(dirname($path)))  
  45.             return false;  
  46.         ftp_mkdir($this->conn_id,$path);  
  47.         return true;  
  48. }  
  49. //验证是否为目录  
  50. //对$path进行验证:如果是目录返回true,否则返回false。  
  51. function ftp_is_dir($path)  
  52. {  
  53.     $original_directory = ftp_pwd($this->conn_id);  
  54.     if(@ftp_chdir($this->conn_id,$path))  
  55.     {  
  56.         ftp_chdir($this->conn_id,$original_directory);  
  57.         return true;  
  58.     }  
  59.     else  
  60.         return false;  
  61. }  
  62. //功能:上传文件  
  63. //$ftppath:存在ftp服务器位置;$localpath:本地文件位置;  
  64. //上传成功返回true,否则返回false。  
  65. function uploadfile($ftppath = null, $localpath = null)   
  66. {  
  67.     if(!emptyempty($ftppath) && !emptyempty($localpath))   
  68.     {  
  69.         $ret = ftp_nb_put($this->conn_id, $ftppath$localpath, FTP_BINARY);  
  70.         while ($ret == FTP_MOREDATA)   
  71.         {  
  72.             $ret = ftp_nb_continue ($this->conn_id);  
  73.         }  
  74.         if ($ret != FTP_FINISHED)   
  75.         {  
  76.             $this->message( "上传失败");  
  77.             return false;  
  78.         }   
  79.         else   
  80.         {  
  81.             $this->message("上传成功");  
  82.             return true;  
  83.         }  
  84.     }  
  85. }  
  86. //功能:删除目录  
  87. //$dir:要删除的目录  
  88. //删除成功返回true,否则返回false。  
  89. function deldir($dir = null)   
  90. {  
  91.     if (ftp_rmdir($this->conn_id, $dir))   
  92.     {  
  93.         $this->message("删除目录成功");  
  94.         return true;  
  95.     }   
  96.     else   
  97.     {  
  98.         $this->message("删除目录失败");  
  99.         return false;  
  100.     }  
  101. }  
  102. //功能:返回目录  
  103. //返回当前目录名称  
  104. function redir()  
  105. {  
  106.     return ftp_pwd($this->conn_id);  
  107. }  
  108. //功能:删除文件  
  109. //$path:文件路径  
  110. //删除成功返回true,否则返回false。  
  111. function delfile($path = null)   
  112. {  
  113.     if(ftp_delete($this->conn_id, $path))  
  114.     {  
  115.         $this->message("删除文件成功");  
  116.         return true;  
  117.     }   
  118.     else   
  119.     {  
  120.         $this->message("删除文件失败");  
  121.         return false;     
  122.     }  
  123. }  
  124. //功能:打印信息  
  125. //$str:要打印的信息  
  126. function message($str = null)   
  127. {  
  128.     if(!emptyempty($str))   
  129.     {  
  130.         echo $str;  
  131.     }  
  132. }  
  133. //功能:关闭ftp连接  
  134. function closeftp()   
  135. {  
  136.     ftp_close($this->conn_id);  
  137. }  
  138.   
  139. }   
  140. /* 
  141. 一下为示范; 
  142. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
  143. <?php 
  144. include 'ftp.php'; 
  145. $conn=new Net_FTP(); 
  146. //$ftppath=$conn->redir(); 
  147. $ftppath='/test'; 
  148. $locpath="/home/liye/public_html/php_ftp/test"; 
  149. //$conn->uploadfile($ftppath,$locpath); 
  150. //$conn->newdir('test/123/1233'); 
  151. //$conn->deldir('test/123/1233'); 
  152. //$conn->delfile($ftppath); 
  153. $conn->delfile('ftp.php'); 
  154. ?> 
  155.  
  156.  
  157. */  

相关内容