skip to Main Content

I want to upload a file from Laravel to another server using FTP.

It seems a very simple task, so let’s take a look at my configurations:

.env file

FTP_HOST=dl.myserver.com
[email protected]
FTP_PASSWORD=somePass

filesystem.php

    'disks' => [

        'local' => [
            'driver' => 'local',
            'root' => storage_path('app'),
        ],

        'ftp' => [
            'driver' => 'ftp',
            'host' => env('FTP_HOST'),
            'username' => env('FTP_USERNAME'),
            'password' => env('FTP_PASSWORD'),
            'passive'  => true,
            'port' => 21,
            'root' => '/home/myserver/public_html/podcasts'
        ],
       .
       .
       .

and my controller finally

        $year = Carbon::now()->year;
        $month = Carbon::now()->month;
        $day = Carbon::now()->day;

        //podcast
        $podcast = $request->file('podcast');
        $filename = $podcast->getClientOriginalName();
        $purename = substr($filename, 0, strrpos($filename, '.'));


        $filenametostore = $purename . '_' . $year .'_' . $month . '_' . $day . '.' . $podcast->getClientOriginalExtension();

        Storage::disk('ftp')->put($filenametostore, fopen($request->file('podcast'), 'r+'));

.
.
.

but I have this error:

LeagueFlysystemConnectionRuntimeException

Could not log in with connection: dl.myserver.com::21, username:
[email protected]

My FTP account and information is true because I logged in using FileZilla.

As a mention, my dl.server.com is using CPANEL.

Is there any Idea about this issue?

Thanks in Advance

2

Answers


  1. Chosen as BEST ANSWER

    Surprisingly the problem solved when I replaced env('FTP_HOST'), env('FTP_USERNAME') and env('FTP_PASSWORD') with equivalent string values in filesystems.php file!

    I tried pure PHP FTP functions and figured it out:

            $conn_id = ftp_connect("dl.myserver.com");
            ftp_login($conn_id, "[email protected]", "somePass");
            dd(ftp_put($conn_id, $filenametostore, $request->file('podcast'), FTP_ASCII));
    
    

    So my Laravel filesystem.php looks like this:

            'ftp' => [
                'driver' => 'ftp',
                'host' => "dl.myserver.com", //env('FTP_HOST'),
                'username' => "[email protected]", //env('FTP_USERNAME'),
                'password' => "somePass", //env('FTP_PASSWORD'),
            ],
    
    

    and it works fine in my case.


  2. You need to put the password with double quotes in your .env
    Particularly if your password contains spaces or #

    FTP_PASSWORD="some#Pass"

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search