php使用ftp

2020-09-02

windows使用ftp需要配置php.ini:extension=php_ftp 

注:php_ftp.dll默认存在php的ext目录中

<?php

class Ftp{
    private $_host;//远程服务器地址
    private $_user;//ftp用户名
    private $_pass;//ftp密码
    private $_port = 21;//ftp登录端口
    private $_error;//最后失败时的错误信息
    protected $_conn;//ftp登录资源

    /**
     * Ftp constructor.
     * @param array $config
     */
    public function __construct(array $config)
    {
        $this->initialize($config);
        $this->connect();
    }

    public function __destruct()
    {
        @ftp_close($this->_conn);
    }

    /**
     * 初始化数据
     * @param array $config 配置文件数组
     */
    protected function initialize(array $config=[]){
        $this->_host = $config['host'];
        $this->_user = $config['user'];
        $this->_pass = $config['pass'];
        $this->_port = isset($config['port']) ?: 21;
    }

    /**
     * 连接及登录ftp
     * @return bool
     */
    protected function connect(){
        if (false == ($this->_conn = @ftp_connect($this->_host))){
            $this->_error = "主机连接失败";
            return false;
        }
        if (!$this->_login()){
            $this->_error = "服务器登录失败";
            return false;
        }
        return true;
    }

    /**
     * 上传文件到ftp服务器
     * @param string $local_file 本地文件路径
     * @param string $remote_file 服务器文件地址
     * @param string $mode 上传模式(ascii和binary其中之一)
     */
    public function upload($local_file,$remote_file,$mode='auto'){
        if (!file_exists($local_file)){
            $this->_error = "本地文件不存在";
            return false;
        }
        $mode = $this->_get_mode($local_file,$mode);
        $result = @ftp_put($this->_conn,$remote_file,$local_file,$mode);//同步上传
        if ($result === false){
            $this->_error = "文件上传失败";
            return false;
        }
        return true;
    }

    /**
     * 从ftp服务器下载文件到本地
     * @param string $local_file 本地文件地址
     * @param string $remote_file 远程文件地址
     * @param string $mode 上传模式(ascii和binary其中之一)
     */
    public function download($local_file,$remote_file,$mode='auto'){
        $mode = $this->_get_mode($remote_file,$mode);
        $result = @ftp_get($this->_conn, $local_file, $remote_file, $mode);
        if ($result === false){
            return false;
        }
        return true;
    }

    /**
     * 获取文件的后缀名
     * @param string $local_file
     */
    private function _get_ext($local_file=''){
        return (($dot = strrpos($local_file,'.'))==FALSE) ? 'txt' : substr($local_file,$dot+1);
    }

    /**
     * 根据文件后缀获取上传编码
     * @param string $ext
     */
    private function _set_type($ext=''){
        //如果传输的文件是文本文件,可以使用ASCII模式,如果不是文本文件,最好使用BINARY模式传输。
        return in_array($ext, ['txt', 'text', 'php', 'phps', 'php4', 'js', 'css', 'htm', 'html', 'phtml', 'shtml', 'log', 'xml'], TRUE) ? 'ascii' : 'binary';
    }

    /**
     * 根据文件和上传模式获取应当使用的上传模式
     * @param string $ext
     */
    private function _get_mode($file,$mode='auto'){
        if ($mode == 'auto'){
            $ext = $this->_get_ext($file);
            $mode = $this->_set_type($ext);
        }
        return ($mode == 'ascii') ? FTP_ASCII : FTP_BINARY;
    }

    /**
     * 登录Ftp服务器
     */
    private function _login(){
        return @ftp_login($this->_conn,$this->_user,$this->_pass);
    }

    /**
     * 获取上传错误信息
     */
    public function get_error_msg(){
        return $this->_error;
    }
}

方法来源于:https://www.cnblogs.com/phproom/p/9683612.html

{/if}