• PHP中ftp的连接与操作


     1.操作类

    <?php
    class FtpService
    {
    protected $connect = 0;
    public function __construct()
    {
    $this->connect = $this->openServer();
    if(($this->connect === 0) || ($this->connect === 1)) return $this->connect;
    }

    /**
    * 打开并登录服务器
    *
    * @return mixed
    * 0:服务器连接失败
    * 1:服务器登录失败
    * resource 连接标识
    */
    public function openServer(){
    //选择服务器
    $config = config('ftp.'); //ftp配置

    //连接服务器
    $connect = ftp_connect($config['host'], $config['port']);
    if($connect == false) return false;

    //登录服务器
    if(!ftp_login($connect, $config['user'], $config['pwd'])){
    return false;
    }else{
    echo "connect success!"; echo "<br>";
    }

    //打开被动模式,数据的传送由客户机启动,而不是由服务器开始
    ftp_pasv($connect, false);

    //返回连接标识
    return $connect;
    }

    /**
    * 创建目录并将目录定位到当请目录
    *
    * @param resource $connect 连接标识
    * @param string $dirPath 目录路径
    * @return mixed
    * 2:创建目录失败
    * true:创建目录成功
    */
    public function makeDir($dirPath){
    $dirPath = '/' . trim($dirPath, '/');
    $dirPath = explode('/', $dirPath);
    foreach ($dirPath as $dir){
    if($dir == '') $dir = '/';
    //判断目录是否存在

    if(!@ftp_chdir($this->connect, $dir)){
    //判断目录是否创建成功
    if(@ftp_mkDir($this->connect, $dir) == false){
    return false;
    }
    @ftp_chdir($this->connect, $dir);
    }
    }

    //关闭服务器
    $this->closeServer($this->connect);
    return true;
    }

    /**
    * 关闭服务器
    *
    * @param resource $connect 连接标识
    */
    public function closeServer($connect){
    if(!empty($connect)) ftp_close($connect);
    }

    /**
    * 上传文件
    *
    * @param string $flag 服务器标识
    * @param string $local 上传文件的本地路径
    * @param string $remote 上传文件的远程路径
    * @return int
    */
    public function upload($local, $remote){
    //上传文件目录处理
    $mdr = $this->makeDir(dirname($remote));
    if($mdr === false) return false;

    if (!file_exists($local)){
    return false;
    }

    $connect = $this->openServer();
    //上传文件
    $result = ftp_put($connect, $remote, $local, FTP_BINARY);
    //关闭服务器
    $this->closeServer($connect);

    //返回结果
    return (!$result) ? false : true;
    }

    /**
    * 删除文件
    *
    * @param string $flag 服务器标识
    * @param string $remote 文件的远程路径
    * @return int
    */
    public function delete($remote){
    //删除
    $result = ftp_delete($this->connect, $remote);
    //关闭服务器
    $this->closeServer($this->connect);

    //返回结果
    return (!$result) ? false : true;
    }

    /**
    * 读取文件
    *
    * @param string $flag 服务器标识
    * @param string $remote 文件的远程路径
    * @return mixed
    */
    public function read($remote){
    //读取
    $result = ftp_nlist($this->connect, $remote);

    //关闭服务器
    $this->closeServer($this->connect);

    echo "<pre>";
    print_r($result);exit;
    //返回结果
    foreach ($result as $key => $value){
    if(in_array($value, array('.', '..'))) unset($result[$key]);
    }
    return array_values($result);
    }

    /**
    * 下载文件
    */
    public function down($local,$remote){
    $result = ftp_get($this->connect,$local,$remote,FTP_BINARY);
    $this->closeServer($this->connect);
    return $result;
    }
    }

    2.ftp连接配置:
    <?php
    return [
    'host' => 'IP',
    'port' => 21,
    'user' => '账号',
    'pwd' => '密码'
    ];
    3.ftp操作调用测试: ftp目录结构(IP/homes/,下面是在homes目录下测试)
        public function ftp_test(){
    $path = iconv('UTF-8','GB2312',$_SERVER['DOCUMENT_ROOT']."/uploads/photos/test.txt");
    $ftp = new FtpService();

    // $result = $ftp->delete("homes/test.txt"); //删除操作
    // $result = $ftp->read("homes"); //读操作
    // $result = $ftp->down("test.txt",'/homes/test.txt'); //下载操作
    // $result = $ftp->makeDir('/homes/testdir/'); //创建文件夹

    $result = $ftp->upload($path,'/homes/test1.txt'); //上传操作

    echo $result;
    }
    添加ftp扩展:php.ini文件添加:extension=php_ftp.dll



  • 相关阅读:
    js获得动态生成的标签
    自定义字段在List和ClassList等标签里的使用方法
    asp.net dropdownlist 取不到值
    MXCMS新增标签IFrame 包含标签
    JS打印
    Flash图表解决方案 Finger Chart
    推荐个免费的客户端控件
    C#利用反射获取对象属性值
    位置导航MXCMS Position标签说明
    OAuth简介
  • 原文地址:https://www.cnblogs.com/luqiang213917/p/13941027.html
Copyright © 2020-2023  润新知