PHP ftp连接方法

时间:2014-07-09 03:13:52

标签: php ftp

我使用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。有什么建议?          }

1 个答案:

答案 0 :(得分:0)

更改此

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';
  }

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

我发表评论的一些内容是我不确定的,因为我没有完整的课程来告诉他们默认情况下应该是什么。