CakePHP 5 uploaded file validation always failing

47 Views Asked by At

Trying to validate an uploaded file in CakePHP 5

 $validator
            ->notEmptyFile('thumbnail_upload', 'an image is required')
            ->requirePresence('thumbnail_upload', 'create')
            ->add('thumbnail_upload', [
                'mimeType' => [
                    'rule' => [
                        'mimeType', [
                            'image/jpeg',
                            'image/png',
                            'image/jpg'
                        ],
                    ],
                    'message' => 'File type must be either .jpg, .jpeg or .png' 
                ],
            ]);

And whilst the files in question are valid, this step in the validator always fails. There's no errors with the \Laminas\Diactoros\UploadedFile or anything else, it's as far as I can tell the validator. This is the file upload form input

    <?= $this->Form->control('image_upload', [
        'type' => 'file',
        'accept' => 'image/jpeg, image/jpg, image/png'
    ]) ?>

Completely stuck and not sure why, I've tried using the uploadedFile() validation method but that still gets the error.

1

There are 1 best solutions below

0
SamWalker On

The issue was I was moving the file before validating the entity. This caused the moved boolean to change to true, which was causing the fail. Commented out code is where I originally had it, below the patch is where it needed to be.

// if ($imageFile->getError() === 0) {
            //     $imageFile->moveTo(WWW_ROOT . "img/articles/"  .  $datetime->format('Y-m-d-H-i-s') . '-' . $imageFile->getClientFilename());
            //     $data['image'] = 'articles/' . $datetime->format('Y-m-d-H-i-s') . $imageFile->getClientFilename();
            // }
            // if ($thumbnailFile->getError() === 0) {
            //     $thumbnailFile->moveTo(WWW_ROOT . "img/articles/" . $datetime->format('Y-m-d-H-i-s') . '-' . $thumbnailFile->getClientFilename());
            //     $data['thumbnail'] = 'articles/' . $datetime->format('Y-m-d-H-i-s') . $thumbnailFile->getClientFilename();
            // }

            $data['thumbnail'] = 'articles/' . $datetime->format('Y-m-d-H-i-s') . $thumbnailFile->getClientFilename();
            $data['image'] = 'articles/' . $datetime->format('Y-m-d-H-i-s') . $imageFile->getClientFilename();

            $article = $this->Articles->patchEntity($article, $data, [
                'contain' => ['ArticlesCategories']
            ]);

            if ($article->hasErrors(true) === false) {
                if ($imageFile->getError() === 0) {
                    $imageFile->moveTo(WWW_ROOT . "img/articles/"  .  $datetime->format('Y-m-d-H-i-s') . '-' . $imageFile->getClientFilename());
                }
                if ($thumbnailFile->getError() === 0) {
                    $thumbnailFile->moveTo(WWW_ROOT . "img/articles/" . $datetime->format('Y-m-d-H-i-s') . '-' . $thumbnailFile->getClientFilename());
                }
            }

I was originally confused as I had an almost identical thing somewhere else, however that was uploading things to an S3 bucket, so in the eyes of Laminas\Diactoros\UploadedFile, this wasn't being moved, and so passed validation.