DEV Community

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

Posted on • Edited on

2 2

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 :)

Neon image

Build better on Postgres with AI-Assisted Development Practices

Compare top AI coding tools like Cursor and Windsurf with Neon's database integration. Generate synthetic data and manage databases with natural language.

Read more →

Top comments (0)

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, cherished by the supportive DEV Community. Coders of every background are encouraged to bring their perspectives and bolster our collective wisdom.

A sincere “thank you” often brightens someone’s day—share yours in the comments below!

On DEV, the act of sharing knowledge eases our journey and forges stronger community ties. Found value in this? A quick thank-you to the author can make a world of difference.

Okay