CakePHP3 - 如何将图像的上传大小限制为3mb?

I'm trying to limit the upload size of the images that the user can add to the db, How can i do this and show a ("warning message") when the image the user is trying to upload is more than 3mb?

Here is my controller code.

public function add()
{
    $blog = $this->Blogs->newEntity();
    if ($this->request->is('post')) {

         if (!empty($this->request->data['mainimg'])) {
            $file = $this->request->data['mainimg'];
            $destinationPath = WWW_ROOT . 'blogs_img' . DS;
            $filename = $this->request->data['mainimg']['name'];
            $extension = pathinfo($filename, PATHINFO_EXTENSION);
            $arr_ext = array('jpg', 'jpeg','png', 'JPG');

           if (!in_array($extension, $arr_ext)) {

                $this->Flash->adminerror(__('Incorrect Image Format.. Please Try Again'));

            }else{

            $uniqueFileName = time().'_'.mt_rand(10000000, 99999999).'_'.mt_rand(10000000, 99999999).'.'.$extension;
            move_uploaded_file($file['tmp_name'], $destinationPath . '/' . $uniqueFileName);
            $filePath = '/blogs_img/' . $uniqueFileName; 

            }
        }

        $blog = $this->Blogs->patchEntity($blog, $this->request->data);
        $blog->mainimg = $filePath;

        if ($this->Blogs->save($blog)) {
            $this->Flash->adminsuccess(__('The blog has been saved.'));

            return $this->redirect(['action' => 'index']);
        } else {
            $this->Flash->adminerror(__('The blog could not be saved. Please, try again.'));
        }
    }
    $users = $this->Blogs->Users->find('list', ['limit' => 200]);
    $cattypes = $this->Blogs->Cattypes->find('list', ['limit' => 200]);
    $this->set(compact('blog', 'users', 'cattypes'));
    $this->set('_serialize', ['blog']);
}

See Validation::fileSize() and simply use it when validating.

There are even more file related validation rules like mime type included.