php的ftp类

1.需求

了解php的ftp使用

2.例子

使用CI封装好的ftp类库

上传
$this->load->library('ftp');

$config['hostname'] = 'ftp.example.com';
$config['username'] = 'your-username';
$config['password'] = 'your-password';
$config['debug']    = TRUE;

$this->ftp->connect($config);
//路径要绝对路径
$this->ftp->upload('/local/path/to/myfile.html', '/public_html/myfile.html', 'ascii', 0775);

$this->ftp->close();
下载文件列表
$this->load->library('ftp');

$config['hostname'] = 'ftp.example.com';
$config['username'] = 'your-username';
$config['password'] = 'your-password';
$config['debug']    = TRUE;

$this->ftp->connect($config);

$list = $this->ftp->list_files('/public_html/');

print_r($list);

$this->ftp->close();

 3.自己写的class

<?php

class ftp{
    public $hostname = '';
    public $username = '';
    public $password = '';
    public $port = 21;
    public $passive = TRUE;
    public $debug = FALSE;
    protected $conn_id;

    public function __construct($config =array())
    {
        if(count($config)>0)
        {
            $this->initialize($config);
        }
    }

    public function initialize($config)
    {
        foreach ($config as $key => $val)
        {
            if (isset($this->$key))
            {
                $this->$key = $val;
            }
        }
        $this->hostname = preg_replace('|.+?://|', '', $this->hostname);
    }

    public function connect($config = array())
    {
        if (count($config) > 0)
        {
            $this->initialize($config);
        }

        if (FALSE === ($this->conn_id = @ftp_connect($this->hostname, $this->port)))
        {
            if ($this->debug === TRUE)
            {
                echo "ftp_unable_to_connect";
                exit();
            }
            return FALSE;
        }

        if ( ! $this->_login())
        {
            if ($this->debug === TRUE)
            {
                echo "ftp_unable_to_login";
                exit();
            }

            return FALSE;
        }

        if ($this->passive === TRUE)
        {
            ftp_pasv($this->conn_id, TRUE);
        }
        return TRUE;
    }

    protected function _login()
    {
        return @ftp_login($this->conn_id, $this->username, $this->password);
    }


    protected function _is_conn()
    {
        if ( ! is_resource($this->conn_id))
        {
            if ($this->debug === TRUE)
            {
                echo 'ftp_no_connection';
                exit();
            }

            return FALSE;
        }
        return TRUE;
    }


    public function changedir($path, $suppress_debug = FALSE)
    {
        if ( ! $this->_is_conn())
        {
            return FALSE;
        }

        $result = @ftp_chdir($this->conn_id, $path);

        if ($result === FALSE)
        {
            if ($this->debug === TRUE && $suppress_debug === FALSE)
            {
                echo 'ftp_unable_to_changedir';
                exit();
            }

            return FALSE;
        }

        return TRUE;
    }

    public function mkdir($path, $permissions = NULL)
    {
        if ($path === '' OR ! $this->_is_conn())
        {
            return FALSE;
        }

        $result = @ftp_mkdir($this->conn_id, $path);

        if ($result === FALSE)
        {
            if ($this->debug === TRUE)
            {
                echo 'ftp_unable_to_mkdir';
                exit();
            }

            return FALSE;
        }

        if ($permissions !== NULL)
        {
            $this->chmod($path, (int) $permissions);
        }

        return TRUE;
    }

    public function upload($locpath, $rempath, $mode = 'auto', $permissions = NULL)
    {
        if ( ! $this->_is_conn())
        {
            return FALSE;
        }

        if ( ! file_exists($locpath))
        {
            echo 'ftp_no_source_file';
            return FALSE;
        }

        if ($mode === 'auto')
        {
            // Get the file extension so we can set the upload type
            $ext = $this->_getext($locpath);
            $mode = $this->_settype($ext);
        }

        $mode = ($mode === 'ascii') ? FTP_ASCII : FTP_BINARY;

        $result = @ftp_put($this->conn_id, $rempath, $locpath, $mode);

        if ($result === FALSE)
        {
            if ($this->debug === TRUE)
            {
                echo 'ftp_unable_to_upload';
                exit();
            }

            return FALSE;
        }
        if ($permissions !== NULL)
        {
            $this->chmod($rempath, (int) $permissions);
        }

        return TRUE;
    }

    public function download($rempath, $locpath, $mode = 'auto')
    {
        if ( ! $this->_is_conn())
        {
            return FALSE;
        }

        if ($mode === 'auto')
        {
            // Get the file extension so we can set the upload type
            $ext = $this->_getext($rempath);
            $mode = $this->_settype($ext);
        }

        $mode = ($mode === 'ascii') ? FTP_ASCII : FTP_BINARY;

        $result = @ftp_get($this->conn_id, $locpath, $rempath, $mode);

        if ($result === FALSE)
        {
            if ($this->debug === TRUE)
            {
                echo 'ftp_unable_to_download';
                exit();
            }

            return FALSE;
        }

        return TRUE;
    }

    public function delete_file($filepath)
    {
        if ( ! $this->_is_conn())
        {
            return FALSE;
        }

        $result = @ftp_delete($this->conn_id, $filepath);

        if ($result === FALSE)
        {
            if ($this->debug === TRUE)
            {
                echo 'ftp_unable_to_delete';
                exit();
            }
            return FALSE;
        }

        return TRUE;
    }

    public function chmod($path, $perm)
    {
        if ( ! $this->_is_conn())
        {
            return FALSE;
        }

        if (@ftp_chmod($this->conn_id, $perm, $path) === FALSE)
        {
            if ($this->debug === TRUE)
            {
                echo 'ftp_unable_to_chmod';
                exit();
            }

            return FALSE;
        }

        return TRUE;
    }

    protected function _getext($filename)
    {
        return (($dot = strrpos($filename, '.')) === FALSE)
            ? 'txt'
            : substr($filename, $dot + 1);
    }


    protected function _settype($ext)
    {
        return in_array($ext, array('txt', 'text', 'php', 'phps', 'php4', 'js', 'css', 'htm', 'html', 'phtml', 'shtml', 'log', 'xml'), TRUE)
            ? 'ascii'
            : 'binary';
    }

    public function close()
    {
        return $this->_is_conn()
            ? @ftp_close($this->conn_id)
            : FALSE;
    }
}
$obj = new ftp();

$config['hostname'] = 'http://xia.com';
$config['username'] = 'web';
$config['password'] = 'web';
$config['debug']    = TRUE;

$obj->connect($config);
//路径要绝对路径
$obj->upload("C:\\Users\\Administrator\\Desktop\\e.txt", '/www/3.txt', 'ascii', 0775);

$obj->close();

 

参考资料:

http://codeigniter.org.cn/user_guide/libraries/ftp.html

http://php.net/manual/zh/book.ftp.php

 

转载于:https://www.cnblogs.com/norm/p/6262473.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值