Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel validation allow image or pdf

I have a file input in Laravel and I would like it to only allow images or pdf. How do I do this in controller validation?

$validator = Validator::make($request->all(), [
    'image' => 'image',
]);
like image 212
Paul Lucero Avatar asked Sep 13 '16 10:09

Paul Lucero


People also ask

How do I know if an image is PDF or laravel?

You can simply use an OR statement, i.e. return ($file['content-type'] == 'image/*' || $file['content-type'] == 'application/pdf'); This is assuming you still just want to return true/false. So the caller will know that the file was either a PDF or an image.

How to validate image file in laravel?

so you can also follow image upload with form validation in laravel with this code: Laravel Image Upload with Form Validation Example. * Determine if the user is authorized to make this request. * Get the validation rules that apply to the request. * Determine if the user is authorized to make this request.

What is custom validation in laravel?

You can make the code powerful by using the Laravel custom validation rule with parameters. This streamlines the flow between the logic and code wherever required. Laravel offers various convenient validation rules that can be applied to the data if values are specific to a database table.

What is form validation in laravel?

Validation is the most important aspect while designing an application. It validates the incoming data. By default, base controller class uses a ValidatesRequests trait which provides a convenient method to validate incoming HTTP requests with a variety of powerful validation rules.


1 Answers

You should rather make a MIME type validation:

$validator = Validator::make($request->all(), [
    'image' => 'mimes:jpeg,bmp,png,gif,svg,pdf',
]);
like image 190
Hammerbot Avatar answered Oct 17 '22 18:10

Hammerbot