Table of contents
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 :)
Read next