php 代码操作ftp类

<?php
/********************************************
*FTP类
*******************************************/
namespace ftp;
class ftp
{
    public $off;             // 返回操作状态(成功/失败)
    public $conn_id;           // FTP连接
    public $FTP_HOST = 'xxxxxxx';
    public $FTP_PORT = 21;
    public $FTP_USER = 'xxxx';
    public $FTP_PASS = 'xxxx';
    /**
    * 方法:FTP连接
    * @FTP_HOST -- FTP主机
    * @FTP_PORT -- 端口
    * @FTP_USER -- 用户名
    * @FTP_PASS -- 密码
    */
    function __construct()
    {
    $this->conn_id = @ftp_connect($this->FTP_HOST,$this->FTP_PORT) or die("FTP服务器连接失败");
    @ftp_login($this->conn_id,$this->FTP_USER,$this->FTP_PASS) or die("FTP服务器登陆失败");
    }
    function ftpUplode($fileName,$localPath,$serverPath,$type=true)
    {
        if($type) $this->dir_mkdirs($serverPath);
        $this->off = @ftp_put($this->conn_id,$serverPath.$fileName,$localPath,FTP_BINARY);
        if(!$this->off) echo "文件上传失败,请检查权限及路径是否正确!";
    }
    /**
     * 方法:删除文件
     * @path -- 路径
     */
    function del_file($serverPath)
    {
        $this->off = @ftp_delete($this->conn_id,$serverPath);
        if(!$this->off) echo "文件删除失败,请检查权限及路径是否正确!";
    }


    /**
     * 方法:生成目录
     * @path -- 路径
     */

    function dir_mkdirs($path)
    {
        $path_arr = explode('/',$path);       // 取目录数组
        $file_name = array_pop($path_arr);      // 弹出文件名
        $path_div = count($path_arr);        // 取层数
        foreach($path_arr as $val)          // 创建目录
        {
            if(@ftp_chdir($this->conn_id,$val) == FALSE)
            {
                $tmp = @ftp_mkdir($this->conn_id,$val);
                if($tmp == FALSE)
                {
                    echo "目录创建失败,请检查权限及路径是否正确!";
                    exit;
                }
                @ftp_chdir($this->conn_id,$val);
            }
        }
        for($i=1;$i<=$path_div;$i++)         // 回退到根
        {
            @ftp_cdup($this->conn_id);
        }
    }
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值