DEV Community

Cover image for How to Get File Extension on Uploaded file in Laravel 8
Code And Deploy
Code And Deploy

Posted on • Updated on

How to Get File Extension on Uploaded file in Laravel 8

Originally posted @ https://codeanddeploy.com visit and download the sample code: https://codeanddeploy.com/blog/laravel/how-to-get-file-extension-on-uploaded-file-in-laravel-8

Advanced Laravel SAAS Starter Kit with CRUD Generator

Advanced Laravel SAAS Starter Kit with CRUD Generator - GET YOUR COPY NOW!

In this post, I will share how to get a file extension on uploaded files in Laravel 8. We usually get the file extension of an uploaded file to customize the file name and append the extension file.

I will share with you a simple example of how to do it.

First, create your post route.

Route::post('/files/add', 'FilesController@store')->name('files.store');
Enter fullscreen mode Exit fullscreen mode

Then in your controller let's create a store method.

/**
* Store a newly created resource in storage.
*
* @param  Request  $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
    echo $request->file->extension();
}
Enter fullscreen mode Exit fullscreen mode

Then next, your form view.

<form action="{{ route('files.store') }}" method="post" enctype="multipart/form-data">
     @csrf
     <div class="form-group mt-4">
         <input type="file" name="file" class="form-control" accept=".jpg,.jpeg,.bmp,.png,.gif,.doc,.docx,.csv,.rtf,.xlsx,.xls,.txt,.pdf,.zip">
     </div>
     <button class="w-100 btn btn-lg btn-primary mt-4" type="submit">Save</button>
</form>
Enter fullscreen mode Exit fullscreen mode

Advanced Laravel SAAS Starter Kit with CRUD Generator

Advanced Laravel SAAS Starter Kit with CRUD Generator - GET YOUR COPY NOW!

I hope this tutorial can help you. Kindly visit here https://codeanddeploy.com/blog/laravel/how-to-get-file-extension-on-uploaded-file-in-laravel-8 if you want to download this code.

Happy coding :)

Top comments (0)