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?