Codeigniter 4 Image upload setup

PHOTO EMBED

Fri Aug 21 2020 14:40:19 GMT+0000 (UTC)

Saved by @brainyworld #php #codeigniter4

       $pic = $this->request->getFile('image');
            if ($pic->isValid()) {
                //let set the rules
                $profile_pic = [
                    'photo' => [
                        'label' => 'Image', 'rules' => 'uploaded[image]|max_size[image,2048]|is_image[image]',
                        'errors' => [
                            'max_size' => 'The uploaded image not accepted',
                            'is_image' => 'Image format not accepted'
                        ]
                    ]
                ];

                //now let validate the picture
                if (!$this->validate($profile_pic)) {
                    //let store the error here
                    $errors = array(
                        'error' => $validation->listErrors('error_msg')
                    );
                    //show the error
                    session()->setFlashdata($errors);
                    return redirect()->back()->withInput();
                } else {
                    //this is to get random
                    $name = $pic->getRandomName();
                    $pic->move('./assets/images/blog/', $name);
                }
content_copyCOPY

Image upload