mojoblanco
mojoblanco

Reputation: 723

How can i get the correct file extension in laravel 5.4

When I upload a csv file in laravel 5.4, I get 'txt' as the extension. Is there something I'm missing?

View

{!! Form::open(['url' => 'transaction/save', 'files' => true]) !!}

{!! Form::file('batch'); !!}

{!! Form::submit('Upload') !!}

{!! Form::close() !!}

Controller

public function saveBatch(Request $request)
 {
        $file = $request->batch;
        $path = $request->batch->extension();

        dd($path);
 }

Upvotes: 8

Views: 27241

Answers (2)

apokryfos
apokryfos

Reputation: 40730

You need to move the file first, if you don't then the file is actually a temp file with no extension. You could also use:

 $request->batch->getClientOriginalExtension();

This would return the original filename's extension. More methods at: http://api.symfony.com/3.0/Symfony/Component/HttpFoundation/File/UploadedFile.html

Upvotes: 16

Volkan Metin
Volkan Metin

Reputation: 178

The best way to get the extension of uploaded file: (If file input name is file)

$extension = $request->file->extension();

Upvotes: 2

Related Questions