【问题标题】:PHP ftp connect methodPHP ftp 连接方法
【发布时间】:2014-07-09 03:13:52
【问题描述】:

我正在使用 PHP 在 ftp 上上传文件。我创建了一个类来创建目录/上传文件等。我正在传递这样的 ftp 登录详细信息

$ftp_server="";
$ftp_user="";
$ftp_pass="";

//variable that connects to the FTP server
$connect = ftp_connect('',21,120);

//logins into FTP server account
ftp_login($connect, $ftp_user, $ftp_pass); 

$uploader = new Uploader();
$pusher->Uploader("abc.php","abc_feeds.php",$connect);

这工作正常,但我想在我的 Uploader 类中添加 ftp_connect 函数,然后将 $this->ftpConnect 传递给这样的方法

    private function connect(){

            if (!isset($this->ftp)){
                $ftpConn= ftp_connect('',21,120) or die ("Cannot connect to host");
                $this->ftpConnect = $ftpConn;
                $ftpLogin=ftp_login($ftpConn, $this->ftp["user"], $this->ftp["pass"]) or die("Cannot login, wrong username or password");
                ftp_pasv($this->ftp, true);
                $this->status = 'Connected';
            }
        }

     public function upload($filePath, $desPath) {
    ....
     if (ftp_mkdir($this->ftpConnect, $this->ftpDrop)) {
     echo "successfully created $dir\n";
    } else {
        echo "Error"; 
    ....
     }

但问题是 $this->ftpConnect 传递的是 null。有什么建议么? }

【问题讨论】:

  • 什么是 $this->ftpConnect,我假设它是一个属性,它在哪里定义。您也将错误的变量传递给此, ftp_pasv($this->ftp... $this->ftp is a configuration array

标签: php ftp


【解决方案1】:

改变这个

if (!isset($this->ftpConnect)){
                $ftpConn= ftp_connect('',21,120) or die ("Cannot connect to host");
                $this->ftpConnect = $ftpConn;
                $ftpLogin=ftp_login($ftpConn, $this->ftp["user"], $this->ftp["pass"]) or die("Cannot login, wrong username or password");
                ftp_pasv($this->ftp, true);
                $this->status = 'Connected';
            }

  if (!isset($this->ftp)){ // what is this? shouldn't it be if(!$this->ftpConnect)
        $this->ftpConnect = ftp_connect('',21,120) or die ("Cannot connect to host");
        $ftpLogin=ftp_login($this->ftpConnect, $this->ftp["user"], $this->ftp["pass"]) or die("Cannot login, wrong username or password"); //$ftpLogin is never used and can be removed because of the die( )
        ftp_pasv($this->ftpConnect, true);
        $this->status = 'Connected';
  }

说明:
$this->ftp 是您的配置数组,您可以通过 $this->ftp["user"] 看到,而不是连接资源,一旦您将值分配给类属性,您不应该在实例变量上中继本地方法范围,因为它会使代码更加混乱,只需分配它然后使用它。

有一些我不确定的东西,因为我没有完整的类来准确地告诉他们默认情况下应该是什么。

【讨论】:

    猜你喜欢
    • 2013-02-09
    • 1970-01-01
    • 2011-04-13
    • 1970-01-01
    • 2011-09-26
    • 2011-01-11
    • 2014-03-25
    • 2010-12-01
    • 1970-01-01
    相关资源
    最近更新 更多