0

I'm working to create a custom ftp cross platform client using electron and php. I've created a simple php class that is supposed to manage the ftp connection, files upload and deletion and now I'm woking to implement it inside an electron app. I've a problem When I try to prompt a login form to the user before it can upload files. After the user insert the ftp login credentials, using an ajax request it's supposed to doing the login. The problem is that the login will not occur and this will cause that the upload is impossible. The workflow is that when the first post resuest is made, the wrapper class that is managing the login is instantiated, then after this, the upload form is loaded inside the app, but no upload occurs. Using the network tab I can inspect the requests and the controller I've made will always give a 200 status code, no error is logged. I think the problem is because The connection is not passed across the requests. Is it possible to pass a variable across $_POST requests? Sorry for my dumb question and for the english. Thanks for the help.

here is a snippet of my code:

Controller for ajax requests:

<?php
session_start();

spl_autoload_register(function($class_name){
  $class_name = str_replace('//', DIRECTORY_SEPARATOR,  $class_name);
  require_once "$class_name.php";
});

global $ftp_connection;
global $ftp_manager;

if( isset( $_POST['ftp_login'] ) ){
  $ftp_connection = new FTPConnect( $_POST['host'], $_POST['username'], $_POST['password'] );
}

if( isset( $_POST['upload_file'] ) ){
  $ftp_manager = new FTPManager( $ftp_connection );
  $dir = 'sub.mydomain.net';
  echo $ftp_manager->upload($_FILES['uploaded_file'], $dir);
}


?>

Ftp connection wrapper class code:

<?php
/**
 *
 */
class FTPConnect{

  private $connection;
  private $host;
  private $username;
  private $password;

  public function __construct(string $host, string $username, string $password)
  {
    // If I try to assign the resource it will not work
    //$this->connection = ftp_connect($host);
    $this->conn = $this->connect( $username, $password );
  }

  public function getConnection()
  {
    return $this->connection;
  }

  public function connect(string $host, string $username, string $password)
  {
    //$this->connection = ftp_connect($host);
      if( $this->connection && ftp_login($this->connnection, $user, $password) ){
        ftp_pasv( $this->connection, true );
        echo "Connected.!";
      }
  }

  public function disconnect()
  {
    return ftp_close($this->connection);
  }

  public function __destruct()
  {
    $this->disconnect();
  }

}

?>

EDIT:

I think that there is a mistake inside the connection wrapper class. I can serialize and unserialize the object inside the session variable, but I have a problem with the ftp management class. The problem is that it will need a resource and the connection wrapper class is passing an integer. How I can fix this to pass the ftp_connect resource to the upload method of the class?

AshDesen
  • 1
  • 1
  • The global variable is not kept accross calls. You have to reconnect on every request / script run. You can also try to store the connection inside `$_SESSION`, however, I'm not sure if this will work and even if, if it is a good idea to do so. – Kryptur May 20 '19 at 11:31
  • I've not tried to use the `$_SESSION` variable, this because the variable will hold an object and I don't think that the session can manage objects? – AshDesen May 20 '19 at 11:36
  • It can hold objects. They are serialized. Cf. [here](https://stackoverflow.com/questions/1442177/storing-objects-in-php-session) – Kryptur May 20 '19 at 11:37
  • I need to call `serialize()` or it will be automated? – AshDesen May 20 '19 at 11:43
  • you have to call it according to the link I provided – Kryptur May 20 '19 at 11:47
  • I've tested the serialization, I've noticed that in the second call the upload function is not executed. Any suggestion? – AshDesen May 20 '19 at 12:23
  • Well, if this doesn't work just save the login credentials in the Session and reopen the connection on every call. – Kryptur May 20 '19 at 12:44
  • I've edited the question, I've found a problem with the connection wrapper class I've made – AshDesen May 20 '19 at 12:50
  • You can also try to serialize the connection inside your wrapper. Further, what is `$this->conn`? It's not declared nor used anywhere beside the assignment. – Kryptur May 20 '19 at 12:54
  • It's a mistake, in fact I've removed it. The problem I'm facing is with the `$this->connection` variable that hold the ftp connection resource. It's referenced inside the connect method but the resource need to be available also in other classes and methods. I need to try to move it from the connect method to the costrunt of the connection wrapper so i can call the `getConnection()` method to use the resource? – AshDesen May 20 '19 at 13:01
  • I'm actually not sure what you want to achieve. Where is the problem with reestablishing the connection? I don't know if PHP closes any sockets required for the connection. So starting each call with a clean login should do the trick. If this is working, you can try to further improve your code and check for a session-stored connection in the constructor to set `$this->connection`. – Kryptur May 20 '19 at 13:07
  • PHP will leave the connection open until the `ftp_close()` function is called. So It's not a good solution to make a new connection everytime it's needed if a connection is already active. To fix the code I just need to found a way to pass the `ftp_connect()` resource to the methods that need. I'm trying to assign it inside the constructor but it will not work as expected. – AshDesen May 20 '19 at 13:28
  • Isn't the resource referecene removed by the garbage collector? – Kryptur May 20 '19 at 13:43
  • @Kryptur it's possible, but how to fix it? – AshDesen May 20 '19 at 14:07

0 Answers0