Kiyarash
Kiyarash

Reputation: 2578

FTP connection issue while upload a file using Laravel 7

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:

League\Flysystem\ConnectionRuntimeException

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

Upvotes: 0

Views: 3148

Answers (2)

A. Rincón
A. Rincón

Reputation: 11

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

FTP_PASSWORD="some#Pass"

Upvotes: 1

Kiyarash
Kiyarash

Reputation: 2578

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.

Upvotes: 0

Related Questions