Re: FTP Class Connection Error

From: Date: Fri, 14 Feb 2014 22:53:05 +0000
Subject: Re: FTP Class Connection Error
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-322880@lists.php.net to get a copy of this message
On 02/13/2014 09:58 AM, Ron Piggott wrote:
Good afternoon. I have just begun creating my own classes. I am struggling to understand how to pass the FTP connection ‘resource’ to the various functions. When I run this class the FTP server connection appears to be timing out from an unsuccessful login (with the command “setFtpLogin”). The database functions retrieving server & login credentials are both working and returning the expected values. As I look over my programming to create this e-mail I *think* some of my problem is partially with my use of “$this->connection”. I am unsure of how to correctly code the FTP connection resource within the context of a class. I am receiving the following 2 errors: PHP Warning: ftp_login() expects parameter 1 to be resource, boolean given PHP Warning: ftp_close() expects parameter 1 to be resource, null given For anyone who is able to respond I am not just looking for an answer. I want to learn at the same time. If you are able to give me a small explanation I would very much appreciate it. This is the code which uses the FTP class: <?php # create backup class
     $remote_backup = new remote_backup();
# connect to FTP server
     $ftp_login_server = $remote_backup->getFtpLoginServer( );
     $connection = $remote_backup->setFtpConnection( $ftp_login_server['host'] , $ftp_login_server['port'] , $ftp_login_server['timeout']  );
# login to FTP server
     $ftp_login_credential = $remote_backup->getFtpLoginCredential( );
     $remote_backup->setFtpLogin( $ftp_login_credential['username'] , $ftp_login_credential['password'] );
# disconnect from FTP server
     $remote_backup->closeFtpConnection( );
?> This is the actual class with functions related to the above code: <?php class remote_backup {
     public $connection;
     /**
      * Get FTP Login Server
      *
      * @return mixed
      */
     public function getFtpLoginServer( ) {
The variables used in the following two lines are not set. Remember scope.
         $dsh = "mysql:host=localhost;dbname=" . $MariaDB['db']['database'];
         $dbh = new PDO($dsh, $MariaDB['db']['username'], $MariaDB['db']['password']);
         $query = "SELECT host, port, timeout FROM remote_backup_login WHERE date_effective <= UTC_TIMESTAMP() ORDER BY date_effective DESC LIMIT 1";
         if ($stmt = $dbh->prepare($query)) {
             if ($stmt->execute() or die(print_r($stmt->errorInfo(), true))) {
                 while ($row = $stmt->fetch()) {
                     $ftp_login_server['host'] = stripslashes( $row['host'] );
                     $ftp_login_server['port'] = $row['port'];
                     $ftp_login_server['timeout'] = $row['timeout'];
                 }
             }
         }
         unset($dbh);
         if ( isset( $ftp_login_server ) ) {
             return $ftp_login_server;
         } else {
             return NULL;
         }
     }
     /**
      * Set FTP Connection
      *
      * @param string $host
      * @param int $port
      * @param int $timeout
      *
      * @return $this
      */
     public function setFtpConnection( $host , $port , $timeout ) {
         $this->connection = ftp_connect( $host , $port , $timeout );
         return $this;
     }
No need for the following function. Unless you plan on using the raw connection handler outside of the class.
     /**
      * Get FTP Connection
      *
      * @return mixed
      */
     public function getFtpConnection( ) {
         return $this->connection;
     }
     /**
      * Get FTP Login Credential
      *
      * @return mixed
      */
     public function getFtpLoginCredential( ) {
Same comment as before. The variables used in the following two lines are not set. Remember scope.
         $dsh = "mysql:host=localhost;dbname=" . $MariaDB['db']['database'];
         $dbh = new PDO($dsh, $MariaDB['db']['username'], $MariaDB['db']['password']);
             $query = "SELECT username, password FROM remote_backup_login WHERE date_effective <= UTC_TIMESTAMP() ORDER BY date_effective DESC LIMIT 1";
                 if ($stmt = $dbh->prepare($query)) {
                     if ($stmt->execute() or die(print_r($stmt->errorInfo(), true))) {
                         while ($row = $stmt->fetch()) {
                             $ftp_login_credential['username'] = stripslashes( $row['username'] );
                             $ftp_login_credential['password'] = stripslashes( $row['password'] );
                         }
                     }
                 }
         unset($dbh);
         if ( isset( $ftp_login_credential ) ) {
             return $ftp_login_credential;
         } else {
             return NULL;
         }
     }
     /**
      * Set FTP Login
      *
      * @param string $username
      * @param string $password
      *
      * @return $this
      */
     public function setFtpLogin( $username , $password ) {
don't call the function. Use the class internal variable you set previously. And don't over write your connection variable if ( @ftp_login( $this->connection , $username , $password ) === false ) {
    die("Failed to login with the following: {$username}:{$password});
}
         $this->connection = ftp_login( $this->getFtpConnection() , $username , $password );
         return $this;
     }
     /**
      * Close FTP Connection
      */
     public function closeFtpConnection() {
This should use the internal reference. if ( is_resource($this->connection) ) {
    if ( @ftp_close( $this->connection ) === false ) {
      die('Could not close connection');
    }
}
         ftp_close( $this->getFtpConnection() );
         return;
     }
} ?> Thank you for your help. Ron Ron Piggott www.TheVerseOfTheDay.info
Here is a re-written version of your class. Try it and let me know how it works. <?php class remote_backup { public $C; public function setFtpConnection( $host , $port=21 , $timeout=90 ) {
    if ( ($this->C = ftp_connect( $host , $port , $timeout )) === false ) {
      $this->is_alive();
    }
    return $this;
} public function setFtpLogin( $username , $password ) {
    $this->is_alive();
    if ( @ftp_login( $this->C , $username , $password ) === false ) {
      die("Failed to login with the following: {$username}:{$password}");
    }
    return $this;
} public function closeFtpConnection() {
    $this->is_alive();
    if ( @ftp_close( $this->C ) === false ) {
      die('Could not close connection');
    }
    return $this;
} public function is_alive() {
    if ( $this->C === false ) {
      die('Failed connection...  Connection Handler not found.');
    }
} } $ftp = new remote_backup(); $ftp->setFtpConnection('localhost'); $ftp->setFtpLogin( 'username', 'password'); var_export(ftp_nlist($ftp->C, '.')); $ftp->closeFtpConnection(); -- Jim Lucas http://www.cmsws.com/ http://www.cmsws.com/examples/

« previous php.general (#322880) next »