用户名:  密码:    
中药方大全小图标
关键字:  
您当前的位置:首页 > 其他 > 网站日记

chenzhouyu的ftp操作类---php源代码

提示: 阅读权限:公开  来源:原创  作者:中药方大全
  1. <?php
  2.  
  3. /**
  4.  * FTP操作类
  5.  * @author chenzhouyu
  6.  *
  7.  * 使用$ftps = pc_base::load_sys_class('ftps');进行初始化。
  8.  * 首先通过 $ftps->connect($host,$username,$password,$post,$pasv,$ssl,$timeout);进行FTP服务器连接。
  9.  * 通过具体的函数进行FTP的操作。
  10.  * $ftps->mkdir() 创建目录,可以创建多级目录以“/abc/def/higk”的形式进行多级目录的创建。
  11.  * $ftps->put()上传文件
  12.  * $ftps->rmdir()删除目录
  13.  * $ftps->f_delete()删除文件
  14.  * $ftps->nlist()列出指定目录的文件
  15.  * $ftps->chdir()变更当前文件夹
  16.  * $ftps->get_error()获取错误信息
  17.  */
  18.  
  19. class ftps {
  20.  
  21.     //FTP 连接资源
  22.     private $link;
  23.     //FTP连接时间
  24.     public $link_time;
  25.     //错误代码
  26.     private $err_code = 0;
  27.     //传送模式{文本模式:FTP_ASCII, 二进制模式:FTP_BINARY}
  28.     public $mode = FTP_BINARY;
  29.  
  30.     /**
  31.      * 连接FTP服务器
  32.      * @param string $host       服务器地址
  33.      * @param string $username   用户名
  34.      * @param string $password   密码
  35.      * @param integer $port       服务器端口,默认值为21
  36.      * @param boolean $pasv        是否开启被动模式
  37.      * @param boolean $ssl      是否使用SSL连接
  38.      * @param integer $timeout     超时时间 
  39.      */
  40.     public function connect($host, $username = '', $password = '', $port = '21', $pasv = false, $ssl = false, $timeout = 30) {
  41.         $start = time();
  42.         if ($ssl) {
  43.             if (!$this->link = @ftp_ssl_connect($host, $port, $timeout)) {
  44.                 $this->err_code = 1;
  45.                 return false;
  46.             }
  47.         } else {
  48.             if (!$this->link = @ftp_connect($host, $port, $timeout)) {
  49.                 $this->err_code = 1;
  50.                 return false;
  51.             }
  52.         }
  53.  
  54.         if (@ftp_login($this->link, $username, $password)) {
  55.             if ($pasv)ftp_pasv($this->link, true);
  56.             $this->link_time = time() - $start;
  57.             return true;
  58.         } else {
  59.             $this->err_code = 1;
  60.             return false;
  61.         }
  62.         register_shutdown_function(array(&$this, 'close'));
  63.     }
  64.  
  65.     /**
  66.      * 创建文件夹
  67.      * @param string $dirname 目录名,
  68.      */
  69.     public function mkdir($dirname) {
  70.         if (!$this->link) {
  71.             $this->err_code = 2;
  72.             return false;
  73.         }
  74.         $dirname = $this->ck_dirname($dirname);
  75.         $nowdir = '/';
  76.         foreach ($dirname as $v) {
  77.             if ($v && !$this->chdir($nowdir . $v)) {
  78.                 if ($nowdir)
  79.                     $this->chdir($nowdir);
  80.                 @ftp_mkdir($this->link, $v);
  81.             }
  82.             if ($v)
  83.                 $nowdir .= $v . '/';
  84.         }
  85.         return true;
  86.     }
  87.  
  88.     /**
  89.      * 上传文件
  90.      * @param string $remote 远程存放地址
  91.      * @param string $local 本地存放地址
  92.      */
  93.     public function put($remote, $local) {
  94.  
  95.         if (!$this->link) {
  96.             $this->err_code = 2;
  97.             return false;
  98.         }
  99.         $dirname = pathinfo($remote, PATHINFO_DIRNAME);
  100.         if (!$this->chdir($dirname)) {
  101.             $this->mkdir($dirname);
  102.         }
  103.         if (@ftp_put($this->link, $remote, $local, $this->mode)) {
  104.             return true;
  105.         } else {
  106.             $this->err_code = 7;
  107.             return false;
  108.         }
  109.     }
  110.  
  111.     /**
  112.      * 删除文件夹
  113.      * @param string $dirname  目录地址
  114.      * @param boolean $enforce 强制删除
  115.      */
  116.     public function rmdir($dirname, $enforce = false) {
  117.         if (!$this->link) {
  118.             $this->err_code = 2;
  119.             return false;
  120.         }
  121.         $list = $this->nlist($dirname);
  122.         if ($list && $enforce) {
  123.             $this->chdir($dirname);
  124.             foreach ($list as $v) {
  125.                 $this->f_delete($v);
  126.             }
  127.         } elseif ($list && !$enforce) {
  128.             $this->err_code = 3;
  129.             return false;
  130.         }
  131.         @ftp_rmdir($this->link, $dirname);
  132.         return true;
  133.     }
  134.  
  135.     /**
  136.      * 删除指定文件
  137.      * @param string $filename 文件名
  138.      */
  139.     public function f_delete($filename) {
  140.         if (!$this->link) {
  141.             $this->err_code = 2;
  142.             return false;
  143.         }
  144.         if (@ftp_delete($this->link, $filename)) {
  145.             return true;
  146.         } else {
  147.             $this->err_code = 4;
  148.             return false;
  149.         }
  150.     }
  151.  
  152.     /**
  153.      * 返回给定目录的文件列表
  154.      * @param string $dirname  目录地址
  155.      * @return array 文件列表数据
  156.      */
  157.     public function nlist($dirname) {
  158.         if (!$this->link) {
  159.             $this->err_code = 2;
  160.             return false;
  161.         }
  162.         if ($list = @ftp_nlist($this->link, $dirname)) {
  163.             return $list;
  164.         } else {
  165.             $this->err_code = 5;
  166.             return false;
  167.         }
  168.     }
  169.  
  170.     /**
  171.      * 在 FTP 服务器上改变当前目录
  172.      * @param string $dirname 修改服务器上当前目录
  173.      */
  174.     public function chdir($dirname) {
  175.         if (!$this->link) {
  176.             $this->err_code = 2;
  177.             return false;
  178.         }
  179.         if (@ftp_chdir($this->link, $dirname)) {
  180.             return true;
  181.         } else {
  182.             $this->err_code = 6;
  183.             return false;
  184.         }
  185.     }
  186.  
  187.     /**
  188.      * 获取错误信息
  189.      */
  190.     public function get_error() {
  191.         if (!$this->err_code)
  192.             return false;
  193.         $err_msg = array(
  194.             '1' => 'Server can not connect',
  195.             '2' => 'Not connect to server',
  196.             '3' => 'Can not delete non-empty folder',
  197.             '4' => 'Can not delete file',
  198.             '5' => 'Can not get file list',
  199.             '6' => 'Can not change the current directory on the server',
  200.             '7' => 'Can not upload files'
  201.         );
  202.         return $err_msg[$this->err_code];
  203.     }
  204.  
  205.     /**
  206.      * 检测目录名
  207.      * @param string $url 目录
  208.      * @return 由 / 分开的返回数组
  209.      */
  210.     private function ck_dirname($url) {
  211.         $url = str_replace('', '/', $url);
  212.         $urls = explode('/', $url);
  213.         return $urls;
  214.     }
  215.  
  216.     /**
  217.      * 关闭FTP连接
  218.      */
  219.     public function close() {
  220.         return @ftp_close($this->link);
  221.     }
  222.  
  223. }


tags: chenzhouyu ftp 操作类 php 源代码
返回顶部
推荐资讯
视频:田纪钧讲关节不痛的秘密、膝关节拉筋法
视频:田纪钧讲关节不
白露到了,你还好吗?
白露到了,你还好吗?
尿疗与断食
尿疗与断食
给风疹反复发作女孩的药方(组图)
给风疹反复发作女孩的
相关文章
栏目更新
栏目热门
  1. libreoffice7的命令大全
  2. 帝国cms代码片段备忘录
  3. 帝国cms插件安装模板
  4. 帝国cms插件之标题生成标题图片
  5. 帝国cms全站搜索的分页格式如何修改-流程
  6. 帝国cms中点卡怎么可以直接用来网站的登录
  7. 帝国cms插件之迅搜
  8. php代码判断是不是微信内部浏览器
  9. useragent两千条,爬虫专用
  10. 帝国cms7.2函数大全