MoFish's avatar

File Storage :: Test FTP Connection

Hello,

I am using File Storage (https://laravel.com/docs/8.x/filesystem#introduction) to 'put' a file to a remote ftp server. Everything works great, however I would ideally like to check that I have a decent ftp connection first before uploading, as the credentials are entered via a form and may be invalid occasionally. Could anyone help me understand how this could be achieved?

// configure ftp
$ftp = Storage::createFtpDriver([
            'driver'   => 'ftp',
            'host'     => $web->server_ip,
            'username' => $web->server_username,
            'password' =>  $web->server_password,
            'port'     => 21,
            'passive'  => false,
            'ignorePassiveAddress' => true,
]);

// if connection is good then
$ftp->put('/thankyou.jpg', Storage::disk('local')->get('image1.jpg')); 
// else
// echo 'Something went wrong with the connection';
0 likes
5 replies
automica's avatar
automica
Best Answer
Level 54

@mofish

using a try catch would allow you to determine whether your ftp transfer worked and return an error if it didn't

try{

    $ftp = Storage::createFtpDriver([
        'driver'   => 'ftp',
        'host'     => $web->server_ip,
        'username' => $web->server_username,
        'password' =>  $web->server_password,
        'port'     => 21,
        'passive'  => false,
        'ignorePassiveAddress' => true,
    ]);
    
    // if connection is good then
    $ftp->put('/thankyou.jpg', Storage::disk('local')->get('image1.jpg'));

}
catch (Exception $e){
    return 'Something went wrong with the connection ' . $e->getMessage();
}
MoFish's avatar

I tried the following however instead of catching the error and displaying my own message it is taking me to the laravel debug screen with message like the following:

League\Flysystem\ConnectionRuntimeException
Could not login with connection: ftp.xx.com::21, username: zxxx1

or

League\Flysystem\ConnectionRuntimeException
Could not connect to host: ftp.xxx.com, port:21

Any ideas how I can make it return my message to the screen?

automica's avatar

Have you tried switching the Exception in the catch to one of the two you are getting?

MoFish's avatar

Hi, yes i have tried that however still get the laravel error page appearing.

League\Flysystem\ConnectionRuntimeException
Could not login with connection: ftp.xxx.com::21, username: zxxx 
try{

// upload put here

}catch (ConnectionRuntimeException $e){

echo "boom";

}
zainoz.zaini@gmail.com's avatar

@MoFish - i have successfully try by this method:

<?php

namespace App\Exceptions;
	

   /**
     * Render an exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Exception  $exception
     * @return \Illuminate\Http\Response
     */
    public function render($request, Exception $exception)
    {
        if($exception instanceOf \League\Flysystem\ConnectionRuntimeException){
            //handle custom exception here
            
        }
        return parent::render($request, $exception);
    }
}

Please or to participate in this conversation.