惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

爱范儿
爱范儿
博客园 - 叶小钗
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
腾讯CDC
S
SegmentFault 最新的问题
D
DataBreaches.Net
Hugging Face - Blog
Hugging Face - Blog
L
LangChain Blog
Recent Announcements
Recent Announcements
阮一峰的网络日志
阮一峰的网络日志
N
Netflix TechBlog - Medium
大猫的无限游戏
大猫的无限游戏
M
MIT News - Artificial intelligence
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 三生石上(FineUI控件)
F
Fortinet All Blogs
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Martin Fowler
Martin Fowler
雷峰网
雷峰网
J
Java Code Geeks
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
小众软件
小众软件
云风的 BLOG
云风的 BLOG
T
Tailwind CSS Blog

希仁之拥

领克900半年使用体验 | 希仁之拥的博客 Ubuntu 26.04 Desktop使用体验 | 希仁之拥的博客 【转载】谈谈不受欢迎的博客技术特征 | 希仁之拥的博客 【转载】ClaudeCode 你想知道的所有秘密,源码深度研究报告 | 希仁之拥的博客 2025年年终总结 | 希仁之拥的博客 集成和使用Openclaw后的思考 | 希仁之拥的博客 我买了领克900 | 希仁之拥的博客 服务器性能优化之io拷贝 | 希仁之拥的博客 Go-Sail导航站上线啦 | 希仁之拥的博客 今年国庆的一些感受 [2025] | 希仁之拥的博客 在Deepin 25上配置forticlient | 希仁之拥的博客 分享一些酷酷的站点 [20250908] | 希仁之拥的博客 Go-Sail发布v3.0.6版本了 | 希仁之拥的博客 我对V2EX发布$V2EX讨论的一些感受 | 希仁之拥的博客 如何让Stripe支持支付宝和微信支付 | 希仁之拥的博客 2025上半年里程碑 | 希仁之拥的博客 GitLab+Drone使用体验 | 希仁之拥的博客 四姑娘山之旅 | 希仁之拥的博客 近来帮同事做性能优化的过程回顾 | 希仁之拥的博客 聊聊接口的返回数据结构 | 希仁之拥的博客 由GORM的Updates语法糖 我把 Go-Sail 的文档站更新了 | 希仁之拥的博客 这就是我为什么讨厌拼多多 | 希仁之拥的博客 元旦快乐~ | 希仁之拥的博客 致敬还在写博客的我们 | 希仁之拥的博客 逐步的把图片资源迁移到星光图床上 | 希仁之拥的博客 帮弟弟配了一台mini主机 | 希仁之拥的博客 国庆的一些碎碎念 | 希仁之拥的博客 就这一刻而言,我觉得科技冷冰冰的。 | 希仁之拥的博客 如何使用acme.sh自动续签证书 | 希仁之拥的博客
php使用ftp上传、下载文件 | 希仁之拥的博客
希仁之拥 · 2018-06-27 · via 希仁之拥

示例代码:

<?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