Table of contents

get file extension on uploaded file in laravel 8

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');

 

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();
}

 

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>

 

I hope it helps. Thank you for reading :)

 

Cheers :)