Discussion:
FTP Class Connection Error
Ron Piggott
2014-02-13 17:58:48 UTC
Permalink
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
Aziz Saleh
2014-02-13 20:19:15 UTC
Permalink
On Thu, Feb 13, 2014 at 12:58 PM, Ron Piggott <
Post by Ron Piggott
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.
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.
<?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( );
?>
<?php
class remote_backup {
public $connection;
/**
* Get FTP Login Server
*
*/
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
*
*
*/
public function setFtpConnection( $host , $port , $timeout ) {
$this->connection = ftp_connect( $host , $port , $timeout );
return $this;
}
/**
* Get FTP Connection
*
*/
public function getFtpConnection( ) {
return $this->connection;
}
/**
* Get FTP Login Credential
*
*/
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
*
*
*/
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
Christoph Becker
2014-02-13 20:35:19 UTC
Permalink
Post by Ron Piggott
PHP Warning: ftp_login() expects parameter 1 to be resource, boolean given
[...]
/**
* Set FTP Login
*
*
*/
public function setFtpLogin( $username , $password ) {
$this->connection = ftp_login( $this->getFtpConnection() , $username , $password );
return $this;
}
The return value of ftp_login() is a boolean value, but you're assigning
it to $this->connection. Any further attempt to read $this->connection
will return this boolean value instead of the resource. So you should
not change $this->connection in this method.

Furthermore you should either return the return value of ftp_login()
from this method, or preferably throw an exception if the login fails.
At least, as it's now, you'll never know whether the login succeeded,
and may get hard to find errors in the following.
--
Christoph M. Becker
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Jim Lucas
2014-02-14 22:53:05 UTC
Permalink
Post by Ron Piggott
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.
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.
<?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( );
?>
<?php
class remote_backup {
public $connection;
/**
* Get FTP Login Server
*
*/
public function getFtpLoginServer( ) {
The variables used in the following two lines are not set. Remember scope.
Post by Ron Piggott
$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
*
*
*/
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.
Post by Ron Piggott
/**
* Get FTP Connection
*
*/
public function getFtpConnection( ) {
return $this->connection;
}
/**
* Get FTP Login Credential
*
*/
public function getFtpLoginCredential( ) {
Same comment as before.
The variables used in the following two lines are not set. Remember scope.
Post by Ron Piggott
$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
*
*
*/
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});
}
Post by Ron Piggott
$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');
}
}
Post by Ron Piggott
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/
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Continue reading on narkive:
Loading...