Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
 }
like image 281
mojoblanco Avatar asked Apr 06 '17 08:04

mojoblanco


People also ask

How do I check the extension of a Laravel file?

If you have file object from request then you can simply get by laravel function. $extension = $request->file->extension(); dd($extension); If you have file object from request then you can simply get by laravel function.

What is the extension of Laravel?

Laravel components are generally extended in two ways: binding new implementations in the IoC container, or registering an extension with a Manager class, which are implementations of the "Factory" design pattern.

How can I get file extension in php?

The simplest way to get file extension in PHP is to use PHP's built-in function pathinfo. Save this answer.

How do I change a filename in Laravel?

You can rename your uploaded file as you want . you can use either move or storeAs method with appropiate param. Save this answer. Show activity on this post.


1 Answers

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

like image 84
apokryfos Avatar answered Nov 14 '22 22:11

apokryfos