Laravel Image validation allowing pdf file - php

I am having issues with the file type image validation in Laravel 4. The image validation works if the file mime type is other than application/pdf. Please see the code below:
Controller:
$rules = array(
'title' => 'required|min:3',
'content' => 'required|min:3',
'navigation_title' => 'required|min:3',
'image' => 'image'
);
$validator = Validator::make(Input::all(), $rules);
The view form has enctype="multipart/form-data". The validation works fine if I try to upload txt file, HTML file etc. but validator passes if I upload a PDF file. Do I need to add a mime type validation as well?

Related

Check if image content is a valid image code

Is there any way to check if the image code inside it is valid because
some crackers(hackers) can change the extension and play with image info using some applications to trick the website that the type is image while it's not
so is there any way to check if content is an image code ?
Or is there anyway to check against this type of attacks ?
For Multiple Images
$validatedData = $request->validate([
'images' => 'required',
'images.*' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048'
]);
Single Image Validation
$validatedData = $request->validate([
'image' => 'required',
'image' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048'
]);

Why is my image not storing in a specified folder in Laravel 5.6?

In my form, I asked for an image to upload. Then I already validated it and it works. But the file is not stored in the uploads folder.
Here's a snippet of my ProductController:
public function store(Request $request)
{
// Validate fields
$this->validate($request, [
'product_name' => 'required',
'product_price' => 'required',
'product_desc' => 'required',
'product_img' => 'image|required'
]);
// Upload image
if($request->hasFile('image')) {
app()->make('path.public/uploads');
$image = $request->image;
$image->move('uploads', $image->getClientOriginalName());
}
/*// Save the data into database
Product::create([
'name' => $request->product_name,
'price' => $request->product_price,
'description' => $request->product_desc,
'image' => $request->image->getClientOriginalName()
]);
// Echo a session message
$request->session()->flash('msg', 'Your product has been added');
// Redirect to view page
return redirect('/products');*/
}
I already tried looking at other possible solutions but the other questions were already at storing the image in the database. I also tried checking if uploads was a directory and existed, and it is.
Can anyone please help? Thanks.
Try this: official documentation: This is how it should look in your controller:
if($request->hasFile('image')) {
$request->file('image')->store('uplodads/', 'public');
}
This would store the image in /storage/app/public/uploads by default. You can also change the public path in /config/filesystems.php. You can then access the file (if you linked the storage) with asset('storage/uploads'.$img_name).
app()->make('path.public/uploads');
I've already solved it. The variable was wrong all along. Instead of it being product_img, I placed image.
Here's the updated code:
// Validate fields
$this->validate($request, [
'product_name' => 'required',
'product_price' => 'required',
'product_desc' => 'required',
'product_img' => 'image|required'
]);
// Upload image
if($request->hasFile('product_img')) {
$image = $request->product_img;
$image->move('uploads', $image->getClientOriginalName());
}

Laravel 5.5 "The file must be an image" on image array

I am working on a Laravel 5.5 project which is giving me some issues... One of them is that when I upload some image it returns me an error: "The file must be an image" with the following code in the controller.`
public function update(\App\Property $property, Request $request)
{
$rules = [
'images.*' => 'mimes:image|max:2048',
'zone' => 'required'
];
$messages = [
'images.mimes' => 'The file must be an image', // in my file that is a translated of the original message
'images.max' => 'The images sizes must be under 2MB',
];
$validator = Validator::make($request->all(), $rules, $messages);
if($validator->fails()) {
parent::message(
$validator->errors(),
'¡Error!',
'error'
);
return back()->withInput();
}
Just in case:
I was reading some people who had the same issue but in my case I have the usual <form method="POST action="..." enctype="multipart/form-data">
I made a dd($request->files) on the request but the all the images I upload seems to be images.
Also I've tried to use the iteration method as following:
if($request->hasFile('images')) {
foreach ($images as $image)
{
$validator = Validator::make(
[ 'image' => $image ],
[ 'image' => 'mimes:jpeg,png,jpg,gif,svg|max:2048' ],
[
'image.mimes' => 'Images must have format (JPEG, PNG, JPG, GIF or SVG)',
'image.max' => 'Each image must be under 2MB'
]);
if($validator->fails()) {
parent::message(
$validator->errors(),
'¡Error!',
'error'
);
return back()->withInput();
}
}
}
But when the images are too bigger than 2MB the request is not even passing through if($request->hasFile() function.
And I want a generic error for all the images and not validate every image, is that possible? By the way in the Laravel Documentation it hasn't the previous iterating method.
I was trying to figure this out as well and it seems symfony has been updated since Laravel 5.5. On Laravel 8 I can tell you that the following combination of settings fixed the error I had yielding the same error type you had:
In your form:
make sure your form has
<form method="POST action="..." enctype="multipart/form-data">
In your validator:
'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
In the event that you are using Laravel Collectives for your form, then you can implement the enctype as show below :
{!! Form::open([‘action’ =>’ProfilesController#update’,
‘method’ => ‘POST’, ‘enctype’ => ‘multipart/form-data’]) !!}

Laravel - mimes:pdf not working

I have a form with an input which allows to upload PDFs.
Here is the method that validates my input :
protected function validator(array $data)
{
return Validator::make($data, [
'title' => 'required|max:255|string',
'artist' => 'required',
'notes' => 'nullable|string',
'file' => 'required|mimes:pdf|max:500',
'g-recaptcha-response' => 'required|captcha',
]);
}
The rules are working for all the other fields, but it does not work for the file input. Whatever I upload, PDF or not, I have the following error :
The file must be a file of type: pdf.
I just solved my issue.
In case someone has the same problem, you need to set the enctype attribute of your form to multipart/form-data.
In short, something like this :
<form action="#" enctype="multipart/form-data">
...
</form>
The error is not very clear, so I hope this helps.

Validation doesn't work for image upload in laravel 5

I have problem when validating inputs. All input fields pass the validation process except image fields.
This is my file upload code in html:
<div class="control-group">
<label class="control-label" for="fileInput"> Cover picture: </label>
<div class="controls">
{!! Form::file('cover') !!}
</div>
</div>
And how I get data from view in controller:
$datas = array(
'name' => Input::get('name'),
'color' => Input::get('color'),
'size' => Input::get('size'),
'cover' => array('cover' => Input::file('cover'))
);
And this is rules:
$rules = array(
'name' => 'required',
'color' => 'required',
'size' => 'required',
'cover' => 'required|mimes:jpeg,jpg,png|max:10000'
);
And Validation facades`s make method:
$validator = Validator::make($datas, $rules);
As I mentioned earlier, all validation rules passed for input, but for image it gives me an error:
The cover must be a file of type: jpeg, jpg, png.
Now, how can I fix it?
I think you should approach this a little differently...
If you instead just create your function like so..
// The Illumniate/Http/Request version of Request in laravel
public function yourFunction(Request $request){
....
$rules = ...;
$validator = Validator::make($request->all(), $rules);
}
The expected format of the Validator is handled automatically because it returns it. And when it runs through you can handle everything the same you already have.
Since this is accepted. The way to actually fix it with the code above is to just remove the multi-dimensional array and just use Input::file('cover') as it returns an array on its own.

Categories