1

I am trying to validate multi-select file uploads to Laravel.

I print_r my Request but I don't understand why it doesn't validate when clearly all uploads are images.

The print_r output:

Array ( [file] => Array ( [0] => Illuminate\Http\UploadedFile Object ( [test:Symfony\Component\HttpFoundation\File\UploadedFile:private] => [originalName:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 236x177_3F2.jpg [mimeType:Symfony\Component\HttpFoundation\File\UploadedFile:private] => image/jpeg [size:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 19335 [error:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 0 [pathName:SplFileInfo:private] => /tmp/phpWyxPrW [fileName:SplFileInfo:private] => phpWyxPrW ) [1] => Illuminate\Http\UploadedFile Object ( [test:Symfony\Component\HttpFoundation\File\UploadedFile:private] => [originalName:Symfony\Component\HttpFoundation\File\UploadedFile:private] => Bierstadt_Sunrise_Lg_1966-1[1].jpg [mimeType:Symfony\Component\HttpFoundation\File\UploadedFile:private] => image/jpeg [size:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 113765 [error:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 0 [pathName:SplFileInfo:private] => /tmp/php5GtG4F [fileName:SplFileInfo:private] => php5GtG4F ) ) )

The code:

if($typeof=="photo")
        {
        $photo = $request;
        print_r($photo::all());
        $validator = Validator::make($photo::all(), [
            'file' => 'required|image|mimes:png,gif,jpeg,bmp'
        ]);
        if ($validator->fails()) {
           abort(500,"Not an image!");
        }

It always aborts with the not an image error, even though you can see from the print_r output that they are images.

Looks like a nested array I tried to offset my validation request and then I got undefined offset, please assist me to get to the actual file data for proper validation.

1 Answer 1

3

Well it's not an image, it's an array of images.

You need to validate like this:

if($typeof=="photo")
{
    $photo = $request;
    print_r($photo::all());

    $validator = Validator::make($photo::all(), [
        'file.*' => 'required|image|mimes:png,gif,jpeg,bmp'
    ]);

    if ($validator->fails()) {
       abort(500,"Not an image!");
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.