DEV Community

Cover image for How to Get Client Mime Type on Uploaded file in Laravel 8
Code And Deploy
Code And Deploy

Posted on • Edited on

3 2

How to Get Client Mime Type 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-client-mime-type-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'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');
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->getClientMimeType();
}
Enter fullscreen mode Exit fullscreen mode

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

Advanced Laravel SAAS Starter Kit with CRUD Generator

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

That's pretty much it. I hope this tutorial can help you. Kindly visit here https://codeanddeploy.com/blog/laravel/how-to-get-client-mime-type-on-uploaded-file-in-laravel-8 if you want to download this code.

Happy coding :)

Jetbrains image

Build Secure, Ship Fast

Discover best practices to secure CI/CD without slowing down your pipeline.

Read more

Top comments (0)

Neon image

Next.js applications: Set up a Neon project in seconds

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Get started →

👋 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