laravellaravel-8laravel-9multiple-file-upload

Count total uploaded file in laravel 9 while uploading multiple file


I'm using API for storing images/files in DB. Users can select multiple files. Before storing those files in DB, count the total number and limit the user to uploading more than 4 files.

I have attached the function below

public function apiUserGalleryImageStore(Request $request) {
        $rules = [
            'email' => 'required|email|string',
            'image' => 'required|mimes:jpg,jpeg,png|max:1024',
        ];
        $validator = Validator::make($request->all(), $rules);

        if ($validator->fails()) {
            return response()->json([
                'status' => 422,
                'message' => $validator->errors(),
                'data' => null
            ], 422);
        } else {
            if(!$request->file('image')) {
                return response()->json([
                    'status' => 422,
                    'message' => 'No file founds',
                    'data' => null
                ], 422);
            } else {
                $images = $request->file('image');
                if(count($images) > 3) {
                    return response()->json([
                        'status' => 422,
                        'message' => 'Only 2 files are allowed',
                        'data' => null
                    ], 422);
                } else {
                    foreach ($images as $img) {
                        $imageName = hexdec(uniqid()) . '.' . $img->getClientOriginalName();
                        $directory = 'support-files/images/user/gallery/';
                        $img->move($directory, $imageName);
                        $imageUrl = $directory . $imageName;
                        $ok = UserGallery::insert([
                            'user_id' => 1,
                            'photo_name' => $imageUrl,
                            'created_at' => Carbon::now(),
                        ]);
                        if ($ok == true) {
                            return response()->json([
                                'status' => 200,
                                'error' => 'Photo uploaded successfully',
                                'data' => null
                            ], 200);
                        } else {
                            return response()->json([
                                'status' => 422,
                                'error' => 'Try again',
                                'data' => null
                            ], 422);
                        }
                    }
                }
            }
        }
    }

I'm stuck on count($images) > 3 Here I want a out put like 2,3,4

I have problem with 'image' => 'required|mimes:jpg,jpeg,png|max:1024',

It should be like

**'image'   => 'required|array|max:2',
  'image.*' => 'required|mimes:jpg,jpeg,png|max:1024',**

Solution

  • What you can do is this :

    public function apiUserGalleryImageStore(Request $request) {
            $rules = [
                'email' => 'required|email|string',
                'image'   => 'required|array|min:1|max:4',
                'image.*' => 'required|mimes:jpg,jpeg,png|max:1024',
            ];
            $validator = Validator::make($request->all(), $rules);
    
            if ($validator->fails())
            {
                return response()->json([
                    'status'  => 422,
                    'message' => $validator->errors(),
                    'data'    => null,
                ], 422);
            } else
            {
                $images = $request->file('image');
                $ok = [];
                foreach ($images as $img)
                {
                    $imageName = hexdec(uniqid()).'.'.$img->getClientOriginalName();
                    $directory = 'support-files/images/user/gallery/';
                    $img->move($directory, $imageName);
                    $imageUrl = $directory.$imageName;
                    $ok[] = UserGallery::insert([
                        'user_id'    => 1,
                        'photo_name' => $imageUrl,
                        'created_at' => Carbon::now(),
                    ]);
                }
                // handle here the response, after the foreach
                if(in_array(false, $ok)) {
                   // some files errored
                } else {
                   // all files completed
                }
            }
        }
    

    simply check if you got an array of images like you expect

    Also, check out laravel custom requests, which will allow you to put your validation logic away from your controller, and make things easier