php使用ftp上传、下载文件

示例代码:

<?php

final class FtpHandle
{
    /**
     * [$host ftp host]
     * @var null
     */
    protected $host = null;

    /**
     * [$port ftp port]
     * @var null
     */
    protected $port = null;

    /**
     * [$username ftp username]
     * @var null
     */
    protected $username = null;

    /**
     * [$password ftp password]
     * @var null
     */
    protected $password = null;

    /**
     * [$toDir cd to custom directory]
     * @var null
     */
    protected $toDir = null;

    /**
     * [$remoteFile custom file on remote ftp server]
     * @var null
     */
    protected $remoteFile = null;

    /**
     * [$flink ftp link resource]
     * @var null
     */
    protected $flink = null;

    /**
     * @param $host, $port, $username, $pasword, $timeout
     */
    public function __construct(array $params)
    {
        $this->host = $params[0];
        $this->port = $params[1];
        $this->username = $params[2];
        $this->password = $params[3];
        $this->timeout = $params[4];
    }

    /**
     * @description 建立ftp连接
     * @author      KeepChen
     * @date        2017-06-20
     * @param
     * @param       boolean    $mode [description]
     * @return      [object]         [description]
     */
    public function connect($mode = true)
    {
        //创建连接
        $this->flink = ftp_connect($this->host, $this->port, $this->timeout) or die("can not connect to [{$this->host}]");
        //认证
        $login_result = ftp_login($this->flink, $this->username, $this->password);
        //修改连接方式(true:被动模式)
        ftp_pasv($this->flink, (bool)$mode);
        return $this;
    }

    /**
     * @description 修改目录
     * @author      KeepChen
     * @date        2017-06-20
     * @param
     * @param       [string]     $directory [description]
     * @return      [object]                [description]
     */
    public function cdDir($directory)
    {
        //目录修改
        $chdir = ftp_chdir($this->flink, $directory);
        return $this;
    }

    /**
     * @description 下载远程文件
     * @author      KeepChen
     * @date        2017-06-20
     * @param
     * @param       [string]     $filename [description]
     * @return      [string]               [description]
     */
    public function download($filename)
    {
        $local_setting_file = '/path/to/'.$filename;
        //load到本地临时文件
        $fget = ftp_get($this->flink, $local_setting_file, $filename, FTP_ASCII);
        //返回本地临时文件路径
        return '/path/to/'.$filename;
    }

    /**
     * @description 上传文件
     * @author      KeepChen
     * @date        2017-06-20
     * @param
     * @param       [string]     $filename [description]
     * @return      [boolean]              [description]
     */
    public function upload($filename)
    {
        $local_setting_file = '/path/to/'.$filename;
        //上传本地临时文件到远端
        $fput = ftp_put($this->flink, $filename, $local_setting_file, FTP_ASCII);
        return $fput;
    }
}

调用:

$params = [];
$ftp = new FtpHandle($params);
$filepath = $ftp->connect(true)->cdDir($directory)->download($filename);

转载请注明原文地址:https://blog.keepchen.com/a/php-upload-and-download-files-using-ftp.html