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',
]);
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.
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.
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.
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.
You should rather make a MIME type validation:
$validator = Validator::make($request->all(), [
'image' => 'mimes:jpeg,bmp,png,gif,svg,pdf',
]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With