How to validate files in Laravel? - laravel

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',]);

Related

How to add media conversion to existing media instance

I have my existing $media instance and I would like to generate a thumbnail at $seconds.
I tried:
$media->manipulations = [
'thumb' => [
'height' => '250',
'extractVideoFrameAtSecond' => $seconds,
],
];
$media->save();
But get this error:
Can not convert image. Unknown operation extractVideoFrameAtSecond used

Laravel Image validation not working when I try to upload ai or psd file

Laravel Image validation not working. When I try to upload ai or PSD file it's showing error:
Illuminate \ Http \ Exceptions \ PostTooLargeException No message
Also not working when trying to upload a 3Mb image file.
$this->validate($request, [
'company_name' => 'required',
'logo' => 'mimes:jpeg,jpg,png|max:1024|image',
'address' => 'required',
'phone' => 'required|numeric',
'currency_code' => 'required',
'vat_rate' => 'required|numeric'
]);
Default file upload size is 2MB
Open the php.ini file. Find these lines in the php.ini file and replace it following numbers: upload_max_filesize = 64M Save the changes and try uploading the file again. You will now get success.
You can find the path of your PHP configuration file in your xampp/php/php.ini(Windows User) file And don't forget to restart your server
Try using size validation Rule instead of max, from the docs:
size:value
The field under validation must have a size matching the given value.
For string data, value corresponds to the number of characters. For
numeric data, value corresponds to a given integer value. For an
array, size corresponds to the count of the array. For files, size
corresponds to the file size in kilobytes.
$this->validate($request, [
'company_name' => 'required',
'logo' => 'mimes:jpeg,jpg,png|size:1024|image',
'address' => 'required',
'phone' => 'required|numeric',
'currency_code' => 'required',
'vat_rate' => 'required|numeric'
]);

PHPUnit laravel in a form with multiple uploaded file

I've a problem, I'm trying test my aplicattion with PHPUnit, in that aplicattion I've multiple forms where the user can upload files, and since Admin can upload differents files, I'm trying test a form where the admin can upload an image, and an audio, but I don't get it; here's my code:
public function testCreateAudiobooks()
{
Storage::fake('uploads/audiobooks/cover_pages');
Session::start();
$response = $this->json('POST', 'admin-audiobooks', [
'audiobook_title' => 'Audiolibro con Unit',
'audiobook_description' => 'DescripciĆ³n de audiolibro desde Unit',
'audiobook_author' => 'PHPUnit',
'audiobook_section' => 'Pruebas',
'audiobook_keywords' => 'Unit',
'audiobook_storyteller' => 'Luis',
'audiobook_duration' => '15',
'audiobook_visibility' => 'Visible',
'audiobook_cover_page' => UploadedFile::fake()->image('45.png'),
'audiobook_location' => UploadedFile::fake(),
'_token' => csrf_token()
]);
Storage::disk('uploads/audiobooks/cover_pages')->assertMissing('45.png');
$response->assertRedirect();
}
As you can see, I pass the UploadedFile in audiobook_cover_page, but I dunno how can pass something similar for the audiobook_location
I found this artcile, and I try do something similar, but still didn't
If someone can help me, I'll be really gratefull!

Image validation rule not working as expected

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',

image type validation and filesize validation using cakephp

Hi i have this validaton for the images that validates only jpg and png types and also i aadded a filesize that is only 5mb. I want that when i will not select an image it will bypass. So no validation for selecting images. What happend is when i did not select an image it will validates to select valid image. How will i able to do this that when im not selecting an image it will not validate. Heres my validation below
'images' => [
'extension' => [
'rule' => [
'extension',
['png', 'jpg']
],
'allowEmpty'=>['Empty'],
'message' => 'Select valid image'
],
'fileSize' =>[
'rule'=>
'fileSize',
['<=', '5mb'],
'message' => 'Image must be less than 5mb'
]
],
this is just a simple one. Any help is muchly appreciated.

Resources