Shivamyadav's avatar

File upload validation error

my validation for the logo

'logo' => 'required|mimes:jpeg,png,jpg,svg|max:2048',

even I'm using the enctype in form and uploading the proper file with extension with .png,.jpg etc downloaded from the internet . I get the validation error

The logo field must be a file of type: jpeg, png, jpg, svg.

Yesterday! everything was working fine , everything related to file upload is now seems broken since I've installed a cloudinary package. Any solution?

0 likes
3 replies
jamesbuch79's avatar

It might help to know which cloudinary package you installed.

First, Is the input type file, with name logo, and multipart/form-data?

Maybe try: 'logo' => 'required|file|image|mimes:jpeg,png,jpg,svg|max:2048'

Explicity putting file and image validation rules together along with required, otherwise if that doesn't work, just required|image|mimes,.... The message seems to be suggesting logo field must be a file, so perhaps there's something missing there - it's not seeing that logo is a file/file of image type, maybe if it's integrating with Laravel you need to do something else, maybe some configuration option or something, extra code you need in a model or controller to handle files.

Can you dd() in your controller?

if($request->hasFile('logo')) {
    $file = $request->file('logo');
    dd([
        'mime' => $file->getMimeType(),
        'extension' => $file->getClientOriginalExtension(),
        'size' => $file->getSize(),
        'name' => $file->getClientOriginalName()
    ]);
}

But first what cloudinary package did you install?

jamesbuch79's avatar

I don't think it should do anything unless you explicitly say so. It has some macros that extend UploadedFile, for instance ->storeOnCloudinary() and ->storeOnCloudinaryAs().

I would temporarily disable it by commenting it out in config/app.php:

'providers' => [
    // CloudinaryLabs\CloudinaryLaravel\CloudinaryServiceProvider::class,
]

Try again:

 $file = $request->file('logo');
    dd([
        'mime' => $file->getMimeType(),
        'extension' => $file->getClientOriginalExtension(),
        'size' => $file->getSize(),
        'name' => $file->getClientOriginalName()
    ]);

It just seems like a configuration issue, so far, try putting in 'logo' => required|file|image|mimes:...' and give it a shot. See what dd() gives you when you upload something. It just seems like it's not recognising this as an image file. Maybe what you uploaded after installing it wasn't actually a valid image, or was corrupt in some way?

Also what do your laravel logs say?

If you are uploading multiple files, your validation should be something like this:

'logo' => 'array',
'logo.*' => 'image|mimes:jpeg,png,jpg,svg|max:2048'

You should check the client side parameters for correctness, like whether it's multipart/form-data and whether it allows multiple. It should also be of type file.

First check by adding image to your validations, disabling Cloudinary and checking to see if it works again. Then if it works with image in validations, with Cloudinary temporarily disabled, enable it again and try it out. If it fails, let's see what the logs say for Laravel.

Please or to participate in this conversation.