PHP FTP Discussions Creating a class and OOP

Live URL

I have written an FTP class that is able to establish a connection.

class FtpConnection{
  public $host     = '';
  public $user     = '';
  public $password = '';

  public function ftpconn(){
    $ftpConn = ftp_connect($this->host);
    $login = ftp_login($ftpConn,$this->user,$this->password);
    // check connection
    if ((!$ftpConn) || (!$login)) {
        echo 'FTP connection has failed! Attempted to connect to '. $host. ' for user '.$user.'.';
      }else{
        echo 'FTP connection was a success.';
        //Place to use FTP PUT in PHP
      }
      ftp_close($ftpConn);    
    }  
}

$newconn = new FtpConnection;

$newconn->host = '198.57.247.176';
$newconn->user = 'XXXXXXX';
$newconn->password = 'XXXXXX';
echo $newconn->ftpconn();

Question →
Can we improve this class definition making it more OOP in terms of relevance and code quality?
How can we work around making it more secure and using properties more correctly?

For a start, the echos in there don’t make any sense.
You echo the ftpconn method, but the method does not return anything, so it won’t echo anything. For that to work, it would have to return a string, which again, does not make much sense to me.
Then you have more echo within the method. Is it really the job of the method to echo things to the page?

Properties like $password could be made private properties, rather than public ones, then they can’t be accessed outside of the scope of the class. You would then need public methods to set them within the class.

But really it depends upon context. What is this for? How will it be used? What does it need to do?

1 Like

From a big picture point of view, ftp is inherently unsecure. Might try a different sort of example. Maybe sftp.

I find this is easier to understand:

  $msg = 'FTP connection has failed! Attempted to connect to '. $host. ' for user '.$user.'.';
  if ( $ftpConn && $login)
  {
      $msg =  'FTP connection was a success.'; 
   }
   echo $msg;
1 Like

Since you have pointed that out I guess no I will rewrite some parts of the code and paste ity here for further discussion.

Nothing to be launched as a real live project sir. I just wanted to get some understanding in PHP FTP with OOPS. Trying to build a class catalog for FTP classes that can do many things.

Kudos!


  public function ftpconn(){
    $ftpConn = ftp_connect($this->host);
    $login = ftp_login($ftpConn,$this->user,$this->password);
    // check connection
    $msg = 'FTP connection has failed! Attempted to connect to '. $host. ' for user '.$user.'.';
    if ( $ftpConn && $login){
      $msg =  'FTP connection was a success.'; 
    }
    echo $msg;
    ftp_close($ftpConn); 
  }

So concise. thanks.

1 Like

I tried this one →

Still the private property is not accessible through the public function. I wish if this can be work around so it can work.

I also tried this one: Later one I got success, but I think is not very object oriented. Properties are not even used here.

Tried something a refined code:

class FtpConnection{
  public  $host     = '';
  public  $user     = '';
  private $password = '';

  public function __construct($host,$user,$password){
    $this->host     = $host;
    $this->user     = $user;
    $this->password = $password;
  }

  public function ftpconn(){
    $ftpConn = ftp_connect($this->host);
    $login = ftp_login($ftpConn,$this->user,$this->password);
    // check connection
    $msg = 'FTP connection has failed! Attempted to connect to '. $this->host. ' for user '.$this->user.'.';
    if ( $ftpConn && $login){
      $msg = 'FTP connection was a success.'; 
    }
    echo $msg;
    ftp_close($ftpConn); 
  }   
}

$newconn = new FtpConnection('198.57.247.176','toolculator@app.trafficopedia.com','##');
echo $newconn->ftpconn();

This example is better but there is still room for improvement.

  1. The properties can all be private. You do not need to assign an empty string to them.

  2. You have mixed two different functions in your logic. How can you know what failed? Was it the connection that failed? Was it the login that failed? You can’t know the way it is. I will leave it up to you to see what you come up with.

  3. The Success/Fail messages really dont belong in the Class/method. You should just be returning the ftp function return data. Decide how to handle the returns in your page code.

1 Like

Any suggestions or directions?

You test connection and login together, so you can’t know which failed.

if(!$this->ftpConn = ftp_connect($this->host)){
   $this->msg = "Conncetion failed!";
   return false ;
}
if(!$login = ftp_login($this->ftpConn,$this->user,$this->password)){
   $this->msg = "Login failed!";
   return false ;
}

If you test them separate, you can know which failed.
Note I made $ftpConn and $msg into properties of the object.
This means the connection can be reused in the class, outside of the connection method once a connection is made.
If $msg is a public property, it can be accessed outside the class to report on status, eg.

$newconn = new FtpConnection('198.57.247.176','toolculator@app.trafficopedia.com','##');
if(!$newconn->ftpconn()){
    echo $newconn->msg;
}
1 Like

why is $this used here?

Please suggest if this looks good →

  public function ftpconn(){
    $ftpConn = ftp_connect($this->host);
    $login = ftp_login($ftpConn,$this->user,$this->password);
    // check connection and Login 

    if ($ftpConn && $login) {
      $this->msg = 'FTP connection and login was a success.'; 
    } elseif (!$ftpConn) {
        $this->msg = "Conncetion failed!";
        return false ;      
    } else (!$login) {
      $this->msg = "Login failed!";
      return false ; 
    }
    echo $msg;
    ftp_close($ftpConn); 
  }   

Entire class looks like this →

class FtpConnection{
  private  $host;
  private  $user;
  private  $password;
  private  $msg;

  public function __construct($host,$user,$password){
    $this->host     = $host;
    $this->user     = $user;
    $this->password = $password;
  }

  public function ftpconn(){
    $ftpConn = ftp_connect($this->host);
    $login = ftp_login($ftpConn,$this->user,$this->password);
    // check connection and Login 

    if ($ftpConn && $login) {
      $this->msg = 'FTP connection and login was a success.'; 
    } elseif (!$ftpConn) {
        $this->msg = "Conncetion failed!";
        return false ;      
    } else (!$login) {
      $this->msg = "Login failed!";
      return false ; 
    }
    echo $msg;
    ftp_close($ftpConn); 
  }   
}

As you had it, $ftpConn was a variable, which is available only within the scope of that method. When the method has run, the connection is gone.
But the chances are, you would need to use the connection for something (or why connect?).
So I made the connection a property of the class/object, now it will be available for use outside of the scope on that method.

1 Like

This.

1 Like

Noted and understood. Thanks. This property will be private or public?

The final class looks like this →

class FtpConnection{
  private  $host;
  private  $user;
  private  $password;
  private  $msg;
  public   $ftpConn;

  public function __construct($host,$user,$password){
    $this->host     = $host;
    $this->user     = $user;
    $this->password = $password;
  }

  public function ftpconn(){
    $this->ftpConn = ftp_connect($this->host);
    $login = ftp_login($ftpConn,$this->user,$this->password);
    // check connection and Login 

    if ($this->ftpConn && $login) {
      $this->msg = 'FTP connection and login was a success.'; 
    } elseif (!$this->ftpConn) {
        $this->msg = "Conncetion failed!";
        return false ;      
    } else (!$login) {
      $this->msg = "Login failed!";
      return false ; 
    }
    echo $msg;
    ftp_close($ftpConn); 
  }   
}

It depends…
But I would probably make $msg public, so it can be read outside of the object.

1 Like

In a real world situation it is pointless testing for a user and password if their is no connection.

I have not used FTP connection, took a quick look and noticed it is quite involved. Are you sure the remote host has a FTP setup.

Hence this, which was overlooked:-

The connection fails: record that as the status message, return false, the method proceeds no further.

1 Like

A post was split to a new topic: Happy Path Programming