laravelftpflysystem

Make FTP connection in Laravel not working with Flysystem library


Hello i am trying to make a FTP connection, using Laravel 8 and Flysystem library, however all my tries have failed. My ftp details work on Fillezilla

Here is my code

use League\Flysystem\Filesystem;
use League\Flysystem\Adapter\Ftp;

$adapter = new Ftp([
    'host' => $ftp_server,
    'username' => $ftp_username,
    'password' => $ftp_password,
    'port' => 2526,
    'passive' => false,
    'ssl' => false,
    'tls' =>false,
    'timeout' => 400,
    'utf8' => false,
    'passive' => true,
    'transferMode' => FTP_BINARY,
    'systemType' => null, // 'windows' or 'unix'
    'ignorePassiveAddress' => null, // true or false
    'timestampsOnUnixListingsEnabled' => false, // true or false
    'recurseManually' => true // true 
]);



$filesystem = new Filesystem($adapter);


// Check if the connection is successful
if ($filesystem->has($path)) {
    echo "FTP connection successful!";
} else {
    echo "FTP connection failed!";
}

It is giving me always FTP connection failed! and i can't find any solution

Thanks in advance for your help

edit:

I decided to try to do a simple file upload in a simple plain php file to check if it works, and i am having the same problem

for this i used this code

// File details
$fileName = 'example.txt';
$fileContents = 'This is the content of the file.';

// Create a temporary file
$tempFile = tmpfile();
fwrite($tempFile, $fileContents);
fseek($tempFile, 0); // Move the file pointer to the beginning of the file



// Connect to FTP server
$connId = ftp_connect($ftpServer, $ftpPort);

if ($connId) {
    // Login to FTP server
    $loginResult = ftp_login($connId, $ftpUsername, $ftpPassword);

    if ($loginResult) {
        // Perform FTP operations here (e.g., put, get)
       // echo "conexão";
        // Logout and close the connection
        // ftp_pasv($connId, true);
        
        // $localFile = 'C:\xampp\htdocs\FtpTest\file\file.txt';
        // $remoteFile = '/file.txt';
        // //ftp_mkdir($connId, $remoteFile);
        
        // // Upload file to FTP server
        // if (ftp_put($connId, $remoteFile, $localFile, FTP_BINARY)) {
        //     echo 'File uploaded successfully.';
        // } else {
        //     echo 'File upload failed.';
        // }


        // ftp_close($connId);

        // Upload the temporary file to the FTP server

     //   ftp_pasv($connId, true);

$upload = ftp_fput($connId , $fileName, $tempFile, FTP_ASCII);
if (!$upload) {
    die('Failed to upload the file');
}

// Close the temporary file
fclose($tempFile);

// Close FTP connection
ftp_close($ftpConnection);

echo 'File created successfully.';

    } else {
        echo 'FTP login failed.';
    }
} else {
    echo 'Could not connect to FTP server.';
}

If i set my passive as true i get this error

Warning: ftp_fput(): php_connect_nonb() failed: No error (0)

if i set it false i get this error

Warning: ftp_fput(): Illegal PORT command

So i am starting to believe that the problem is not exactly to my code, but maybe something with my server, however i find it strange, since i can enter it , by using filezilla


Solution

  • It is not exactly solved yet, but found out where my problem lies. It is on my server where i am trying to send my files to. Despite a connection was made in filezilla, just found out that it isn't allowing me to transfer files inside there (Since the connection was made, i assumed before, that everything was working fine in there).

    So i believe the code i put in here, and the one that @Hercules73 suggested works fine. Thank you