38

I need to validate multiple uploaded files, making sure they are of a specific type and under 2048kb. The below doesn't appear to check all files in the array 'files' and just presumes the posted files of invalid mime type as it seems to be checking the array object and not its contents.

public function fileUpload(Request $request)
    {

       $validator = Validator::make($request->all(), [
            'files' => 'required|mimes:jpeg,jpg,png',
        ]);

        if ($validator->fails())
        {
            return response()->json(array(
                'success' => false,
                'errors' => $validator->getMessageBag()->toArray()

            ), 400);             }

}

4 Answers 4

81

You can validate files array like any input array in Laravel 5.2. This feature is new in Laravel 5.2.

$input_data = $request->all();

$validator = Validator::make(
    $input_data, [
    'image_file.*' => 'required|mimes:jpg,jpeg,png,bmp|max:20000'
    ],[
        'image_file.*.required' => 'Please upload an image',
        'image_file.*.mimes' => 'Only jpeg,png and bmp images are allowed',
        'image_file.*.max' => 'Sorry! Maximum allowed size for an image is 20MB',
    ]
);

if ($validator->fails()) {
    // Validation error.. 
}
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks, did the trick. I am wondering if there is some shorthand way of also making a rule where all the files in the array can't exceed say 5MB ?
change the max to max:5000
But would that be 5000kb per image file or for the whole array ? I am guessing per file
Yes, it will be for every single image file.
I tried this piece of code in Laravel 6 but is not working. Something changed in Laravel 6?
|
5

Please try this:

public function fileUpload(Request $request) {
    $rules = [];
    $files = count($this->input('files')) - 1;
    foreach(range(0, $files) as $index) {
        $rules['files.' . $index] = 'required|mimes:png,jpeg,jpg,gif|max:2048';
    }

    $validator = Validator::make($request->all() , $rules);

    if ($validator->fails()) {
        return response()->json(array(
            'success' => false,
            'errors' => $validator->getMessageBag()->toArray()
        ) , 400);
    }
}

1 Comment

I tried that It's validating but not passing through $validator->passes()...we're so close
5

We can also make a request and validate it.

php artisan make:request SaveMultipleImages

Here is the code for request

namespace App\Http\Requests;

use App\Core\Settings\Settings;
use Illuminate\Foundation\Http\FormRequest;

class SaveMultipleImages extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
 * Get the validation rules that apply to the request.
 *
 * @return array
 */
    public function rules()
    {
       
        return ['files.*' => "mimes:jpg,png,jpeg|max:20000"];
    }
}

and then in controller

public function uploadImage(SaveMultipleImages $request) {
     dd($request->all()['files']);
}

Comments

2

Try this way.

// getting all of the post data
$files = Input::file('images');

// Making counting of uploaded images
$file_count = count($files);

// start count how many uploaded
$uploadcount = 0;

foreach($files as $file) {
    $rules = array('file' => 'required'); //'required|mimes:png,gif,jpeg,txt,pdf,doc'
    $validator = Validator::make(array('file'=> $file), $rules);
        if($validator->passes()){
            $destinationPath = 'uploads';
                $filename = $file->getClientOriginalName();
                $upload_success = $file->move($destinationPath, $filename);
                $uploadcount ++;
        }
}

if($uploadcount == $file_count){
    //uploaded successfully
}
else {
    //error occurred
}

1 Comment

I used this one. This solution is pretty good if you need to i.e. validate a certain filename or any validation that's more complex than what the validator filters can do.

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.