php ftp类

<meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <?php /* php ftp类主要功能: 1.连接ftp并登陆; 2.创建目录和删除目录; 3.上传文件和删除文件; */ include 'config.php'; class Net_FTP { var $ftp_server; var $ftp_user; var $ftp_pass; var $ftp_port; var $conn_id; function Net_FTP() { $this->ftp_server = server; $this->ftp_user = username; $this->ftp_pass = password; $this->ftp_port = port; // 建立连接 $this->conn_id = ftp_connect($this->ftp_server, $this->ftp_port) or die("不能够连接到 $this->ftp_server"); // 尝试登陆 if (!ftp_login($this->conn_id, $this->ftp_user, $this->ftp_pass)) { $this->message("连接失败 $this->ftp_user"); } else { $this->message("连接成功 $this->ftp_user "); } } //功能:创建新的目录 //$path默认为空目录 //创建成功返回true,否则返回false。 function newdir($path = null) { if($this->ftp_is_dir($this->conn_id,$path)||@ftp_mkdir($this->conn_id,$path)) return true; if(!$this->newdir(dirname($path))) return false; ftp_mkdir($this->conn_id,$path); return true; } //验证是否为目录 //对$path进行验证:如果是目录返回true,否则返回false。 function ftp_is_dir($path) { $original_directory = ftp_pwd($this->conn_id); if(@ftp_chdir($this->conn_id,$path)) { ftp_chdir($this->conn_id,$original_directory); return true; } else return false; } //功能:上传文件 //$ftppath:存在ftp服务器位置;$localpath:本地文件位置; //上传成功返回true,否则返回false。 function uploadfile($ftppath = null, $localpath = null) { if(!empty($ftppath) && !empty($localpath)) { $ret = ftp_nb_put($this->conn_id, $ftppath, $localpath, FTP_BINARY); while ($ret == FTP_MOREDATA) { $ret = ftp_nb_continue ($this->conn_id); } if ($ret != FTP_FINISHED) { $this->message( "上传失败"); return false; } else { $this->message("上传成功"); return true; } } } //功能:删除目录 //$dir:要删除的目录 //删除成功返回true,否则返回false。 function deldir($dir = null) { if (ftp_rmdir($this->conn_id, $dir)) { $this->message("删除目录成功"); return true; } else { $this->message("删除目录失败"); return false; } } //功能:返回目录 //返回当前目录名称 function redir() { return ftp_pwd($this->conn_id); } //功能:删除文件 //$path:文件路径 //删除成功返回true,否则返回false。 function delfile($path = null) { if(ftp_delete($this->conn_id, $path)) { $this->message("删除文件成功"); return true; } else { $this->message("删除文件失败"); return false; } } //功能:打印信息 //$str:要打印的信息 function message($str = null) { if(!empty($str)) { echo $str; } } //功能:关闭ftp连接 function closeftp() { ftp_close($this->conn_id); } } /* 一下为示范; <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <?php include 'ftp.php'; $conn=new Net_FTP(); //$ftppath=$conn->redir(); $ftppath='/test'; $locpath="/home/liye/public_html/php_ftp/test"; //$conn->uploadfile($ftppath,$locpath); //$conn->newdir('test/123/1233'); //$conn->deldir('test/123/1233'); //$conn->delfile($ftppath); $conn->delfile('ftp.php'); ?> */

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值