zoukankan      html  css  js  c++  java
  • php ftp 使用 以及 php_connect_nonb() failed: Operation now in progress (115)

    设置 ftp_pasv($conn,true);  会出现下面错误   不设置 调用ftp 连接没问题  ftp_nlist  ftp_put ftp_get 等函数都不成功

    ftp_nb_fput(): php_connect_nonb() failed: Operation now in progress (115)

    上面问题的解决:

    在ftp_pasv调用前 设置。详细介绍可以看 https://www.php.net/manual/en/function.ftp-set-option.php 

    ftp_set_option($conn,FTP_USEPASVADDRESS,false);

    代码示例:

    <?php 
    date_default_timezone_set('Asia/Shanghai');
    
    // 联接FTP服务器 
    $conn = ftp_connect('39.108.113.37',9009); 
    var_dump($conn);
    // 使用username和password登录 
    ftp_login($conn, 'lexian', 'lx2019'); 
    
    ftp_set_option($conn,FTP_USEPASVADDRESS,false);
    ftp_pasv($conn,true);
    
    // 获取远端系统类型 
    $server_os  = ftp_systype($conn); 
    var_dump($server_os);
    
    //获取当前所在的目录 
    $here = ftp_pwd($conn); 
    var_dump($here);
    // 列示文件 
    $filelist = ftp_nlist($conn, '.'); 
    var_dump($filelist);
    
    //上传文件,ftp_put()函数能很好的胜任,它需要你指定一个本地文件名,上传后的文件名以及传输的类型。比方说:如果你想上传 “abc.txt”这个文件,上传后命名为“xyz.txt”,命令应该是这样: 
    ftp_put($conn, 'errFtp.py', 'err.py', FTP_ASCII); 
    
    // 下载文件 
    ftp_get($conn, 'errFtp', 'errFtp.py', FTP_BINARY); 
    
    //函数ftp_size(),它返回你所指定的文件的大小,使用BITES作为单位。要指出的是,如果它返回的是 “-1”的话,意味着这是一个目录 
    $fileSize = ftp_size($conn, 'errFtp.py'); 
    var_dump($fileSize);
    
    $fileSize = ftp_size($conn, 'dahuaxiyou.mp4'); 
    var_dump($fileSize);
    
    // 关闭联接 
    ftp_quit($conn); 
    

     下面是 tp 的ftp 代码  创建目录 采用了递归 值得看一下

    <?php
    
    namespace ThinkUploadDriver;
    class Ftp {
        /**
         * 上传文件根目录
         * @var string
         */
        private $rootPath;
    
        /**
         * 本地上传错误信息
         * @var string
         */
        private $error = ''; //上传错误信息
    
        /**
         * FTP连接
         * @var resource
         */
        private $link;
    
        private $config = array(
            'host'     => '', //服务器
            'port'     => 21, //端口
            'timeout'  => 90, //超时时间
            'username' => '', //用户名
            'password' => '', //密码
        );
    
        /**
         * 构造函数,用于设置上传根路径
         * @param array  $config FTP配置
         */
        public function __construct($config){
            /* 默认FTP配置 */
            $this->config = array_merge($this->config, $config);
    
            /* 登录FTP服务器 */
            if(!$this->login()){
                E($this->error);
            }
        }
    
        /**
         * 检测上传根目录
         * @param string $rootpath   根目录
         * @return boolean true-检测通过,false-检测失败
         */
        public function checkRootPath($rootpath){
            /* 设置根目录 */
            $this->rootPath = ftp_pwd($this->link) . '/' . ltrim($rootpath, '/');
    
            if(!@ftp_chdir($this->link, $this->rootPath)){
                $this->error = '上传根目录不存在!';
                return false;
            }
            return true;
        }
    
        /**
         * 检测上传目录
         * @param  string $savepath 上传目录
         * @return boolean          检测结果,true-通过,false-失败
         */
        public function checkSavePath($savepath){
            /* 检测并创建目录 */
            if (!$this->mkdir($savepath)) {
                return false;
            } else {
                //TODO:检测目录是否可写
                return true;
            }
        }
    
        /**
         * 保存指定文件
         * @param  array   $file    保存的文件信息
         * @param  boolean $replace 同名文件是否覆盖
         * @return boolean          保存状态,true-成功,false-失败
         */
        public function save($file, $replace=true) {
            $filename = $this->rootPath . $file['savepath'] . $file['savename'];
    
            /* 不覆盖同名文件 */
            // if (!$replace && is_file($filename)) {
            //     $this->error = '存在同名文件' . $file['savename'];
            //     return false;
            // }
    
            /* 移动文件 */
            if (!ftp_put($this->link, $filename, $file['tmp_name'], FTP_BINARY)) {
                $this->error = '文件上传保存错误!';
                return false;
            }
            return true;
        }
    
        /**
         * 创建目录
         * @param  string $savepath 要创建的目录
         * @return boolean          创建状态,true-成功,false-失败
         */
        public function mkdir($savepath){
            $dir = $this->rootPath . $savepath;
            if(ftp_chdir($this->link, $dir)){
                return true;
            }
    
            if(ftp_mkdir($this->link, $dir)){
                return true;
            } elseif($this->mkdir(dirname($savepath)) && ftp_mkdir($this->link, $dir)) {
                return true;
            } else {
                $this->error = "目录 {$savepath} 创建失败!";
                return false;
            }
        }
    
        /**
         * 获取最后一次上传错误信息
         * @return string 错误信息
         */
        public function getError(){
            return $this->error;
        }
    
        /**
         * 登录到FTP服务器
         * @return boolean true-登录成功,false-登录失败
         */
        private function login(){
            extract($this->config);
            $this->link = ftp_connect($host, $port, $timeout);
            if($this->link) {
                if (ftp_login($this->link, $username, $password)) {
                   return true;
                } else {
                    $this->error = "无法登录到FTP服务器:username - {$username}";
                }
            } else {
                $this->error = "无法连接到FTP服务器:{$host}";
            }
            return false;
        }
    
        /**
         * 析构方法,用于断开当前FTP连接
         */
        public function __destruct() {
            ftp_close($this->link);
        }
    
    }
    

      

    下面是 ftp php类  

     https://www.cnblogs.com/phproom/p/9683612.html

    https://blog.csdn.net/a114469/article/details/82784725

  • 相关阅读:
    解析iOS开发中的FirstResponder第一响应对象
    iOS9新特性——堆叠视图UIStackView
    IOS定位服务的应用
    iOS原生地图开发详解
    iOS原生地图开发进阶——使用导航和附近兴趣点检索
    iOS开发之----常用函数和常数
    iOS延时执行
    Mac快捷键、命令行
    UICollectionView使用
    iOS: 在代码中使用Autolayout (1) – 按比例缩放和优先级
  • 原文地址:https://www.cnblogs.com/swing07/p/11382205.html
Copyright © 2011-2022 走看看