Re: FTP Class Connection Error

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Thu, Feb 13, 2014 at 12:58 PM, Ron Piggott <
ron.piggott@xxxxxxxxxxxxxxxxxx> 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( ) {
>
>         $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;
>
>     }
>
>     /**
>      * Get FTP Connection
>      *
>      * @return mixed
>      */
>     public function getFtpConnection( ) {
>
>         return $this->connection;
>
>     }
>
>     /**
>      * Get FTP Login Credential
>      *
>      * @return mixed
>      */
>     public function getFtpLoginCredential( ) {
>
>         $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 ) {
>
>         $this->connection = ftp_login( $this->getFtpConnection() ,
> $username , $password );
>
>         return $this;
>
>     }
>
>     /**
>      * Close FTP Connection
>      */
>     public function closeFtpConnection() {
>         ftp_close( $this->getFtpConnection() );
>
>         return;
>     }
>
> }
>
> ?>
>
> Thank you for your help.
>
> Ron
>
> Ron Piggott
>
>
>
> www.TheVerseOfTheDay.info
>

I can see an issue right off the bat with the setFtpLogin() method.
ftp_login returns a boolean which you are storing (over riding ftp
connection resource) that boolean in the connection variable
($this->connection gets lost).

Aziz

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux