Table of contents

get mime type on uploaded file in laravel

In this post, I'm sharing how to get the client mime type on the uploaded file in Laravel 8? Sometimes we need to determine if what is the mime type of your uploaded file and get the info and save it to your database. Or validate if the mime type of that file is allowed to save on your server.

 

So here's how should 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->getClientMimeType();
}

 

As you can see I call the getClientMimeType() if you upload a PNG image. The result should be like this:

image/png

 

That's pretty much it. I hope it helps. Thank you for reading :)

 

Cheers :)