0
votes

I am uploading file on ftp using PHP. I have created a class to create directory/ upload file etc. I am passing ftp login details like this

$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);

This is working fine but i want to add ftp_connect function inside my Uploader class and then pass $this->ftpConnect into the method like this

    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"; 
    ....
     }

But the problem is $this->ftpConnect is passing null. Any suggestions? }

1
what is $this->ftpConnect, i assume it's a property, where is it defined. also your passing the wrong variable to this, ftp_pasv($this->ftp... $this->ftp is a configuration array - ArtisticPhoenix

1 Answers

0
votes

change this

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

to

  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 is your configuration array as you can see by $this->ftp["user"] , not a connection resource, also once you assign a value to a class property you shouldn't relay on an instance variable in local method scope as it can make the code more confusing, simply assign it and then use it.

There are a few things I put comments in that I wasn't sure of because I don't have the full class to tell exactly what they should be by default.