eidmohd89's avatar

FPT Connection Failed

Hi everyone,

I have created a job that sends a file to an external server over FTP port 21.

below is my code:

  public function sendFile()
    {
        $ftpService = new FtpService();
        $localBasePath = storage_path('app/public/files/');

        try {
            // Connect to FTP server
            $ftpService->connect();

            $filesToUpload = Storage::disk('public')->allFiles('files');

            foreach ($filesToUpload as $file) {
                Log::info('Uploading: ' . basename($file));
                $localPath = sprintf('%s%s', $localBasePath, basename($file));
                if ($ftpService->upload($localPath, basename($file))) {
                    Log::info('Uploaded: ' . basename($file));
                    Storage::disk('public')->delete($file);
                    Log::info('Deleted: ' . basename($file));
                }
            }

            Log::info('All files uploaded successfully!');
        } catch (\Exception $e) {
            Log::error('XML Upload Error: ' . $e->getMessage());
        } finally {
            // Close the FTP connection
            $ftpService->close();
        }
    }

It works well on my test server but fails on production. My website is built on AWS using EC2 instances and a load balancer.

And here is the File system config:

'ftp' => [
            'driver' => 'ftp',
            'host' => env('FTP_HOST'),
            'username' => env('FTP_USERNAME'),
            'password' => env('FTP_PASSWORD'),
            'port' => env('FTP_PORT', 21),
            'root' => env('FTP_ROOT', ''),
            'passive' => true,
            'ssl' => false,
            'timeout' => 30,
],

The outbound rule for both load balancer and EC2 is as bellow:

Type: All traffic
Protocol: All
Port Range: All
Destination: 0.0.0.0/0

For some reason, the connection is unsuccessful and I get the following error on Laravel Log file.

prod.ERROR: XML Upload Error: ftp_put(): php_connect_nonb() failed: Operation now in progress (115)

Any idea what causes the issue. Any recommendation would be appreciated.

Many Thanks

0 likes
0 replies

Please or to participate in this conversation.