用于thinkphp的FTP类(初步实现)

jerry thinkphp 2015年11月18日 收藏
发现官方类库没有FTP库,就随手写了一个,还不完善。
  1. <?php
  2. class FTPAction extends CommonAction{
  3.     public function index(){
  4.         import('ORG.Util.Ftp');
  5.         set_time_limit(0);
  6.         $ftp=new FTP("222.2.2.200",21,"anonymous");
  7.         foreach ($list as $v){
  8.             if (!$ftp->isDir($v))
  9.         $result.= "文件:".pathinfo(iconv('GBK', 'UTF-8', $v),PATHINFO_BASENAME)."<br>";
  10.         else 
  11.          $result.= "目录:<a href='index.html?dir=".iconv('GBK', 'UTF-8', $v)."'>".pathinfo(iconv('GBK', 'UTF-8', $v),PATHINFO_BASENAME)."</a><br>";
  12.         } 
  13.            $this->assign('result',$result);
  14.         $this->display();

  15.     }
  16. }
模板调用{$result}即可。
  1. <?php
  2. /**
  3.  * FTP - 操作FTP文件类.
  4.  *
  5.  * @author     TaoTao
  6.  * @copyright  Copyright (c) 2013 TaoTao
  7.  * @license    New BSD License
  8.  * @conn       http://blog.kisscn.com/
  9.  * @version    1.0
  10.  */
  11. class FTP {
  12.     private  $host;
  13.     private  $port=21;
  14.     private  $user;
  15.     private  $pwd;
  16.     private  $conn;
  17.     private $timeout;
  18.     private $ssl=false;
  19.     //传送模式{文本模式:FTP_ASCII, 二进制模式:FTP_BINARY}
  20.     public $mode = FTP_BINARY;
  21.     public function __construct($host,$port=21,$user,$pwd,$timeout=60,$mode="FTP_BINARY",$ssl=false){
  22.         $this->host=$host;
  23.         $this->port=$port;
  24.         $this->user=$user;
  25.         $this->pwd=$pwd;
  26.         $this->mode=$mode;
  27.         $this->timeout=$timeout;
  28.         $this->ssl=$ssl;
  29.         if($ssl){
  30.         $this->conn=ftp_ssl_connect($this->host,$this->port,$this->timeout) or die("FTP连接失败!");
  31.         }else{
  32.         $this->conn=ftp_connect($this->host,$this->port,$this->timeout) or die("FTP连接失败!");
  33.         }
  34.          ftp_login($this->conn, $user, $pwd) or die("无法打开FTP连接");   
  35.     }

  36.     /**
  37.      * 返回给定目录的文件列表
  38.      * @param string $dirname  目录地址
  39.      * @return array 文件列表数据
  40.      */
  41.     public function nlist($dirname) {
  42.         if ($list = @ftp_nlist($this->conn, $dirname)) {
  43.             return $list;
  44.         }
  45.     }
  46.  /**
  47.   * 返回上级目录
  48.   * @return boolean
  49.   */
  50.  function back_dir()
  51.  {
  52.   return ftp_cdup($this->conn);
  53.  }
  54.  /**
  55.   * 取得指定目录下文件的详细列表信息
  56.   * @param $dirname 目录名称
  57.   * @return ArrayObject
  58.   */
  59.  function get_file_info($dirname)
  60.  {
  61.   $list = @ftp_rawlist($this->conn,$dirname);
  62.   if(!$list) return false;
  63.   $array = array();
  64.   foreach($list as $l)
  65.   {
  66.    $l = preg_replace("/^.*[ ]([^ ]+)$/", "\\1", $l);
  67.    if($l == '.' || $l == '..') continue;
  68.    $array[] = $l;
  69.   }
  70.   return $array;
  71.  }  
  72.     /**
  73.      * 创建文件夹
  74.      * @param string $dirname 目录名,
  75.      */
  76.     public function mkdir($dirname) {
  77.         $dirname = $this->checkDir($dirname);
  78.         $nowdir = '/';
  79.         foreach ($dirname as $v) {
  80.             if ($v && !$this->cd($nowdir . $v)) {
  81.                 if ($nowdir)
  82.                     $this->cd($nowdir);
  83.                 @ftp_mkdir($this->conn, $v);
  84.             }
  85.             if ($v)
  86.                 $nowdir .= $v . '/';
  87.         }
  88.         return true;
  89.     }
  90.  /**
  91.   * 文件和目录重命名
  92.   * @param $old_name 原名称
  93.   * @param $new_name 新名称
  94.   * @return boolean
  95.   */
  96.  function rename($old_name,$new_name)
  97.  {
  98.   return ftp_rename($this->conn,$old_name,$new_name);
  99.  }   
  100.     /**
  101.      * 上传文件
  102.      * @param string $remote 远程存放地址
  103.      * @param string $local 本地存放地址
  104.      */
  105.     public function put($remote, $local) {

  106.         $dirname = pathinfo($remote, PATHINFO_DIRNAME);
  107.         if (!$this->cd($dirname)) {
  108.             $this->mkdir($dirname);
  109.         }
  110.         if (@ftp_put($this->conn, $remote, $local, $this->mode)) {
  111.             return true;
  112.         }
  113.     }
  114.     /**
  115.      * 获取文件的最后修改时间
  116.      * @return string $time 返回时间
  117.      */
  118.     public function lastUpdatetime($file){
  119.         return ftp_mdtm($this->conn,$file);
  120.     }
  121.     
  122.     /**
  123.      * 删除指定文件
  124.      * @param string $filename 文件名
  125.      */
  126.     public function delete($filename) {
  127.         if (@ftp_delete($this->conn, $filename)) {
  128.             return true;
  129.         } 
  130.     }
  131.         
  132.     /**
  133.      * 在 FTP 服务器上改变当前目录
  134.      * @param string $dirname 修改服务器上当前目录
  135.      */
  136.     public function cd($dirname) {
  137.         if (@ftp_chdir($this->conn, $dirname)) {
  138.             return true;
  139.         }
  140.     }   
  141.    /**
  142.      * 在 FTP 服务器上返回当前目录
  143.      * @return string $dirname 返回当前目录名称
  144.      */
  145.     public function getPwd() {
  146.        return ftp_pwd($this->conn);
  147.         
  148.     }    
  149.     /**
  150.      * 检测目录名
  151.      * @param string $url 目录
  152.      * @return 由 / 分开的返回数组
  153.      */
  154.     private function checkDir($url) {
  155.         $url = str_replace('', '/', $url);
  156.         $urls = explode('/', $url);
  157.         return $urls;
  158.     }
  159.     /**
  160.      * 检测是否为目录
  161.      * @param string $dir 路径
  162.      * @return boolean true为目录false为文件
  163.      */   
  164.     public function isDir($dir) {
  165.      if ($this->cd($dir)){
  166.          return true;
  167.      }else{
  168.          return false;
  169.      }
  170.     }
  171.     /**
  172.      * 关闭FTP连接
  173.      */
  174.      
  175.     public function close() {
  176.         return @ftp_close($this->conn);
  177.     }
  178. }