If i want to upload a img it fails. *.png files are ok.
Image sizes are acceptable.
Without validator, *.png are uploaded without any problem.
$validator = Validator::make([
'image' => Input::file('img')
], [
'image' => 'mimes:png, jpg, jpeg | max:5120'
]);
if($validator->fails())
{
return 'Error is occured while uploading file.';
}
Remove the spaces & it should work.
Rename this:
'mimes:png, jpg, jpeg | max:5120'
To this:
'mimes:png,jpg,jpeg|max:5120'
Related
It seems like some ios Devices (newer ones I believe, iPhones and iPads) and versions aren't able to validate files properly.
I'm trying to validate an image, a resume and 3 videos.
I keep getting one of these two errors:
"resume_id": [
"The resume id must be a file of type: doc, docx, pdf."
]
And I always upload a .doc or pdf file.
Or
"video_one_id": [
"The video one id must be a file of type: avi ,mpeg, mpeg4, mov."
]
"video_two_id": [
"The video two id must be a file of type: avi ,mpeg, mpeg4, mov."
]
"video_three_id": [
"The video three id must be a file of type: avi ,mpeg, mpeg4, mov."
]
And I always upload .mov files. I find usually this happens when I take a video on my iphone and upload it. My client also tried the same thing on his brand new iPad and also had no success.
Here is my code.
CandidateProfileRequest.php:
public function rules()
{
return [
'date_of_birth' => 'required',
'experience' => [
'required',
new MaxWordsRule(),
],
'skills' => [
'required',
new MaxWordsRule(350),
],
'job_title' => 'required',
'resume_id' => 'required|file|mimes:doc,docx,pdf',
];
}
CandidateProfileController.php:
public function store(CandidateProfileRequest $request)
{
if ($request->hasFile('video_one_id')
&& $request->hasFile('video_two_id')
&& $request->hasFile('video_three_id')
&& $request->hasFile('photo_id')) {
$request->validate([
'video_one_id' => 'file|mimes:avi ,mpeg, mpeg4, mov|max:30720',
'video_two_id' => 'file|mimes:avi ,mpeg, mpeg4, mov|max:30720',
'video_three_id' => 'file|mimes:avi ,mpeg, mpeg4, mov|max:30720',
'photo_id' => 'image|mimes:jpeg, png, jpg|max:5120'
]);
}
}
I also tried using mimetypes instead of mimes in my validation rules, but still I get the errors above.
On my Macbook Pro Laptop everything works, it's just on my iPhone and my clients iPad.
What am I doing wrong?
you can validate file like this way
$validator = Validator::make([ 'pdf' => $request->file('pdf'), 'extension' => strtolower($request->file('pdf')->getClientOriginalExtension()),
], ['pdf' => 'required', 'extension' => 'required|in:pdf,html,txt', ], ['pdf.required' => 'This field is required', 'extension.required' => 'This field is required','extension.in' => 'This field only accept pdf,html,txt extension',]);
I am trying to upload svg from laravel and it is giving this error.
The icon must be a file of type: png, svg, gif, jpeg.
This is the code of validator.
$rules = array(
'image' => 'mimes:png,gif,svg,jpeg|max:10000' // max 10000kb
);
You can use the
$rules = array(
'image' => 'image'
);
It supports jpeg, png, bmp, gif, or svg file type.
Check this for more info. https://laravel.com/docs/5.4/validation#rule-image
I'm using following code to upload multiple images but I need to add some validation rules like file should be an image with mentioned extensions and file size. Currently it upload everything.
This is my html view code:
<input required="" type="file" name="photos[]" id="photos" multiple="" directory="" webkitdirectory="" mozdirectory="">
This is my controller code:
foreach ($request->photos as $photo) {
$filename = $photo->store('photos');
$data['photoName'] = $filename;
PhotosModel::SavePhotos($data);
$message = "Photos Added Successfully";
}
I've added below code inside foreach loop and before that loop to but it's giving error that photos must be an image and of mentioned types.
$this->validate($request, [
'photos' => 'required|image|mimes:jpeg,png,jpg,gif|max:2048'
]);
I want to upload all images and stop other files from being uploaded. Thanks!
Try this (before your foreach):
$this->validate($request, [
'photos' => 'required',
'photos.*' => 'image|mimes:jpeg,png,jpg,gif|max:2048'
]);
I have to validate otf and ttf font file when uploading ttf working fine but otf not pass the validation using laravel. below given the code sample that fail when i am trying to upload otf file
$validator = Validator::make(Request::file('file'), [
$key => [
'mimes:ttf,application/x-font-otf',
],
]);
if ($validator->fails()) {
return $this->getResponse(Response::HTTP_BAD_REQUEST, $validator->errors()->all()[0]);
}
Here's my validation rule:
public function rules() {
$channel_id = Auth::user()->channels->first()->id;
return [
'name' => 'required|max:30|unique:channel,name,' . $channel_id,
'slug' => 'required|max:30|alpha_num|unique:channel,slug,' . $channel_id,
'description' => 'max:1000',
'channelImage' => 'image',
];
}
public function messages() {
return [
'slug.unique' => 'That unique URL has already been taken.',
'channelImage.image' => 'Only jpeg, jpg, png, bmp, gif and svg formats are supported.',
];
}
Although, is it not mandatory to upload channel image while filling the form, the image validation rule for channelImage works as if it's mandatory i.e. I need to upload an image every time I submit the request.
Why is working like that?? I did not mention required rule for channelImage, still why it is validating channelImage when I am not uploading any image?
Try using nullable:
'channelImage' => 'nullable|image',