2014-07-09 61 views
0

我使用PHP上傳ftp文件。我創建了一個類來創建目錄/上傳文件等我傳遞FTP登錄信息像這樣PHP 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); 

這是工作正常,但我想補充我的上傳類的內部FTP_CONNECT功能,然後通過這 - $> 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。有什麼建議麼? }

+0

什麼是$ this-> ftpConnect,我認爲它是一個屬性,它在哪裏定義。也是你傳遞錯誤的變量到這個,ftp_pasv($ this-> ftp ... $ this-> ftp是一個配置數組 – ArtisticPhoenix

回答

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:
$這個 - > FTP是你的配置數組,你可以通過這個 - $> FTP [「用戶看「],而不是連接資源,同樣,一旦你給一個類屬性賦值,你不應該在本地方法範圍內中繼一個實例變量,因爲它可以使代碼更容易混淆,只需指定它然後我們e it。

有幾件事我把意見,我不知道,因爲我沒有完整的類來確切地告訴他們應該默認。