# Cakephp 5 file uploading validation mimeType does not work

**URL:** <https://discourse.cakephp.org/t/cakephp-5-file-uploading-validation-mimetype-does-not-work/11856>\
**Category:** Uncategorized\
**Created:** [February 8, 2024, 5:23am UTC](https://discourse.cakephp.org/t/cakephp-5-file-uploading-validation-mimetype-does-not-work/11856 "2024-02-08T05:23:42Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![shaan007](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/shaan007/32/2765_2.png) [@shaan007](https://discourse.cakephp.org/u/shaan007)\
**Post date:** [February 8, 2024, 5:23am UTC](https://discourse.cakephp.org/t/cakephp-5-file-uploading-validation-mimetype-does-not-work/11856/1 "2024-02-08T05:23:42Z")

</div>

validation set in model :-

> $validator-\>notEmptyFile(‘featureImage’, \_\_(‘please set feature image!’))  
> -\>add(‘featureImage’, ‘file’, [  
> ‘rule’ =\> [‘mimeType’, [‘image/jpeg’, ‘image/png’]],  
> ‘on’ =\> function ($context) {  
> return !empty($context[‘data’][‘feature\_image’]);  
> }  
> ]);

this code does not check mimetype

---

<div class="post-metadata">

**Author:** ![shadowx.jb](https://avatars.discourse-cdn.com/v4/letter/s/a88e4f/32.png) [@shadowx.jb](https://discourse.cakephp.org/u/shadowx.jb)\
**Post date:** [February 8, 2024, 8:21am UTC](https://discourse.cakephp.org/t/cakephp-5-file-uploading-validation-mimetype-does-not-work/11856/2 "2024-02-08T08:21:48Z")

</div>

Try

```auto
        $validator
            ->allowEmptyFile('image')
            ->requirePresence('image', 'create')
            ->uploadedFile('image', [
                'types' => [
                    'image/png',
                    ‘image/jpeg’,
                ],
            ], __x('Validator', 'Allowed file extension is: .png, .jpg'));

```

---

<div class="post-metadata">

**Author:** ![shaan007](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/shaan007/32/2765_2.png) [@shaan007](https://discourse.cakephp.org/u/shaan007)\
**Post date:** [February 8, 2024, 9:47am UTC](https://discourse.cakephp.org/t/cakephp-5-file-uploading-validation-mimetype-does-not-work/11856/3 "2024-02-08T09:47:03Z")

</div>

hmmm thanks @shadowx.jb but it does not work!! please check it below

> $validator-\>notEmptyFile(‘featureImage’, \_\_(‘please set feature image!’))  
> -\>uploadedFile(‘featureImage’, [  
> ‘types’ =\> [  
> ‘image/jpeg’,  
> ‘image/png’,  
> ],  
> ], \_\_x(‘Validator’, ‘Allowed file extension is: .png, .jpeg’));

 ![Screenshot from 2024-02-08 15-15-48](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/c/c27b641c378a3ee286cb7da8e7614ddaf9896893.png)

---

<div class="post-metadata">

**Author:** ![shadowx.jb](https://avatars.discourse-cdn.com/v4/letter/s/a88e4f/32.png) [@shadowx.jb](https://discourse.cakephp.org/u/shadowx.jb)\
**Post date:** [February 8, 2024, 10:22am UTC](https://discourse.cakephp.org/t/cakephp-5-file-uploading-validation-mimetype-does-not-work/11856/4 "2024-02-08T10:22:27Z")

</div>

What does your `form` look like?

---

<div class="post-metadata">

**Author:** ![shaan007](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/shaan007/32/2765_2.png) [@shaan007](https://discourse.cakephp.org/u/shaan007)\
**Post date:** [February 8, 2024, 10:35am UTC](https://discourse.cakephp.org/t/cakephp-5-file-uploading-validation-mimetype-does-not-work/11856/5 "2024-02-08T10:35:50Z")

</div>

please check it here

 ![Screenshot from 2024-02-08 16-04-52](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/0/0c8abd553d241396c91d374c87f7e9c2a9b47f5a.png)

---

<div class="post-metadata">

**Author:** ![shadowx.jb](https://avatars.discourse-cdn.com/v4/letter/s/a88e4f/32.png) [@shadowx.jb](https://discourse.cakephp.org/u/shadowx.jb)\
**Post date:** [February 8, 2024, 10:52am UTC](https://discourse.cakephp.org/t/cakephp-5-file-uploading-validation-mimetype-does-not-work/11856/6 "2024-02-08T10:52:36Z")

</div>

Form must be

```auto
echo $this->Form->create($article, [
    'enctype' => 'multipart/form-data',
]);

```

not `type => file`

---

<div class="post-metadata">

**Author:** ![shaan007](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/shaan007/32/2765_2.png) [@shaan007](https://discourse.cakephp.org/u/shaan007)\
**Post date:** [February 8, 2024, 11:18am UTC](https://discourse.cakephp.org/t/cakephp-5-file-uploading-validation-mimetype-does-not-work/11856/7 "2024-02-08T11:18:22Z")

</div>

it does not matter @shadowx.jb because [‘type’=\>‘file’] also generator same ectype attribute in html:-

 ![Screenshot from 2024-02-08 16-45-17](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/9/9b258151adfe1820769382cc7b44edf14913bf67.png)

Cakephp also mention that kind of code in Official Docs :-

 ![Screenshot from 2024-02-08 16-48-07](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/5/5b001aa1b743d76f750e92fea9780285b40ec675.png)

---

<div class="post-metadata">

**Author:** ![shadowx.jb](https://avatars.discourse-cdn.com/v4/letter/s/a88e4f/32.png) [@shadowx.jb](https://discourse.cakephp.org/u/shadowx.jb)\
**Post date:** [February 8, 2024, 12:15pm UTC](https://discourse.cakephp.org/t/cakephp-5-file-uploading-validation-mimetype-does-not-work/11856/8 "2024-02-08T12:15:20Z")

</div>

Ok, sorry… try

```auto
        $validator
            ->notEmptyFile('featureImage')
            ->uploadedFile('featureImage', [
                'types' => [
                    'image/jpg',
                    'image/jpeg',
                    'image/png',
                ],
            ], __x('Validator', 'Allowed file extension is: .png, .jpeg, .jpg'));

```

---

<div class="post-metadata">

**Author:** ![shaan007](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/shaan007/32/2765_2.png) [@shaan007](https://discourse.cakephp.org/u/shaan007)\
**Post date:** [February 8, 2024, 12:32pm UTC](https://discourse.cakephp.org/t/cakephp-5-file-uploading-validation-mimetype-does-not-work/11856/9 "2024-02-08T12:32:06Z")

</div>

still getting same thing bro @shadowx.jb

 ![Screenshot from 2024-02-08 17-59-26](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/0/0a8713c24f4536979deb7873e22d3f72c38c500d.png)

 ![Screenshot from 2024-02-08 18-01-20](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/1/13e7b18b291d846d138d82dfbf59bfcf0f7b6364.png)

---

<div class="post-metadata">

**Author:** ![KevinPfeifer](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/kevinpfeifer/32/3365_2.png) [@KevinPfeifer](https://discourse.cakephp.org/u/KevinPfeifer)\
**Post date:** [February 8, 2024, 12:50pm UTC](https://discourse.cakephp.org/t/cakephp-5-file-uploading-validation-mimetype-does-not-work/11856/10 "2024-02-08T12:50:16Z")

</div>

is `featureImage` set as `accessible` inside your Article entity class?

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [February 8, 2024, 2:41pm UTC](https://discourse.cakephp.org/t/cakephp-5-file-uploading-validation-mimetype-does-not-work/11856/11 "2024-02-08T14:41:52Z")

</div>

Your code mostly says `featureImage`, but your entity dump has a field in it called `feature_image`.

---

<div class="post-metadata">

**Author:** ![shadowx.jb](https://avatars.discourse-cdn.com/v4/letter/s/a88e4f/32.png) [@shadowx.jb](https://discourse.cakephp.org/u/shadowx.jb)\
**Post date:** [February 9, 2024, 9:31am UTC](https://discourse.cakephp.org/t/cakephp-5-file-uploading-validation-mimetype-does-not-work/11856/12 "2024-02-09T09:31:16Z")

</div>

The `featureImage` object does not have to be defined in the entity class. I tried the same example and it works for me. Here is the result of `$this->request->getData()`

```auto
[
  'language_id' => '2',
  'user_id' => '8',
  'date' => '2024-02-16',
  'title' => 'hhh',
  'content' => '<p>hh</p>',
  'url' => '',
  'featureImage' => object(Laminas\Diactoros\UploadedFile) id:0 {
    private error => (int) 0
    private file => 'C:\wamp\tmp\php776A.tmp'
    private moved => false
    private stream => object(Laminas\Diactoros\Stream) id:1 {
      protected resource => (resource) Resource id #22
      protected stream => 'C:\wamp\tmp\php776A.tmp'
    }
    private size => (int) 112216
    private clientFilename => 'image-02.jpg'
    private clientMediaType => 'image/jpeg'
  },
]

```

---

<div class="post-metadata">

**Author:** ![shaan007](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/shaan007/32/2765_2.png) [@shaan007](https://discourse.cakephp.org/u/shaan007)\
**Post date:** [February 9, 2024, 2:03pm UTC](https://discourse.cakephp.org/t/cakephp-5-file-uploading-validation-mimetype-does-not-work/11856/13 "2024-02-09T14:03:13Z")

</div>

still same response bro

 ![Screenshot from 2024-02-09 19-30-20](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/1/1ac990f8c78767bb5e7a0c7603f532a4dc8fa87b.png)

---

<div class="post-metadata">

**Author:** ![KevinPfeifer](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/kevinpfeifer/32/3365_2.png) [@KevinPfeifer](https://discourse.cakephp.org/u/KevinPfeifer)\
**Post date:** [February 9, 2024, 2:10pm UTC](https://discourse.cakephp.org/t/cakephp-5-file-uploading-validation-mimetype-does-not-work/11856/14 "2024-02-09T14:10:56Z")

</div>

You’ve shown the form and the validator config but how do you patch the request data into the entity so that the validator is being called.

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [February 9, 2024, 3:06pm UTC](https://discourse.cakephp.org/t/cakephp-5-file-uploading-validation-mimetype-does-not-work/11856/15 "2024-02-09T15:06:23Z")

</div>

I’m still coming back to the fact that the most recent entity dump that you’ve shown us has a field called `feature_image` (along with `article_title`, `article_content`, etc.), but the error is on `featureImage`. Where is `feature_image` coming from, and why does the entity not have a field in it called `featureImage`?

---

<div class="post-metadata">

**Author:** ![shaan007](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/shaan007/32/2765_2.png) [@shaan007](https://discourse.cakephp.org/u/shaan007)\
**Post date:** [February 9, 2024, 3:33pm UTC](https://discourse.cakephp.org/t/cakephp-5-file-uploading-validation-mimetype-does-not-work/11856/16 "2024-02-09T15:33:43Z")

</div>

hi @KevinPfeifer hi @Zuluru hi @shadowx.jb thank’s guys you reply me and try to help me… i am just confused with own code 😁 actually application just working fine only my image validation getting problem… i just want to allow only image content type not file like… php or js or .exe… etc… just author upload only image…  
please check it my screenshots here :-

FORM

 ![Screenshot from 2024-02-09 20-58-36](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/2/22519bdfdbff09fb0cdf704fec107cc2a89b3892.png)

Controller Side:-

 ![Screenshot from 2024-02-09 20-57-36](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/0/01b01c02eec7d45ef1a9a887c239c4e4efa862d4.png)

Validation in model side:-

 ![Screenshot from 2024-02-09 20-58-00](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/4/44ecd2cd0136b09eda718ed62babfd4c8bf31c23.png)

---

<div class="post-metadata">

**Author:** ![shadowx.jb](https://avatars.discourse-cdn.com/v4/letter/s/a88e4f/32.png) [@shadowx.jb](https://discourse.cakephp.org/u/shadowx.jb)\
**Post date:** [February 9, 2024, 3:49pm UTC](https://discourse.cakephp.org/t/cakephp-5-file-uploading-validation-mimetype-does-not-work/11856/17 "2024-02-09T15:49:41Z")

</div>

What does `$this->request->getData()` return to you in the controller?

---

<div class="post-metadata">

**Author:** ![shaan007](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/shaan007/32/2765_2.png) [@shaan007](https://discourse.cakephp.org/u/shaan007)\
**Post date:** [February 9, 2024, 3:53pm UTC](https://discourse.cakephp.org/t/cakephp-5-file-uploading-validation-mimetype-does-not-work/11856/18 "2024-02-09T15:53:45Z")

</div>

even if I allow PNG mimetype but it still throw error… you can see in error index.  
it also throw error when i upload other mimetype image like GIF… obviously throw error because i just allow only PNG JPEG JPG there in validation

 ![Screenshot from 2024-02-08 17-59-26](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/0/0a8713c24f4536979deb7873e22d3f72c38c500d.png)

---

<div class="post-metadata">

**Author:** ![shadowx.jb](https://avatars.discourse-cdn.com/v4/letter/s/a88e4f/32.png) [@shadowx.jb](https://discourse.cakephp.org/u/shadowx.jb)\
**Post date:** [February 9, 2024, 3:57pm UTC](https://discourse.cakephp.org/t/cakephp-5-file-uploading-validation-mimetype-does-not-work/11856/19 "2024-02-09T15:57:39Z")

</div>

Can you please insert the code as a code and not an image?  
Please try to remove this from the entity `featureImage => true`  
And please insert the entire all form code here - not the image.  
I want `$this->request->getData()` to see this right below the POST method.

---

<div class="post-metadata">

**Author:** ![shaan007](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/shaan007/32/2765_2.png) [@shaan007](https://discourse.cakephp.org/u/shaan007)\
**Post date:** [February 9, 2024, 4:06pm UTC](https://discourse.cakephp.org/t/cakephp-5-file-uploading-validation-mimetype-does-not-work/11856/20 "2024-02-09T16:06:59Z")

</div>

Model side validation:-

```auto
$validator->notEmptyFile('featureImage', __('please set feature image!'))
            ->add('featureImage', 'file', [
                'rule' => ['mimeType', ['image/jpeg', 'image/png']],
                'on' => function ($context) {
                    return !empty($context['data']['feature_image']);
                }
            ]);

/* $validator
            ->notEmptyFile('featureImage')
            ->uploadedFile('featureImage', [
                'types' => [
                    'image/jpg',
                    'image/jpeg',
                    'image/png',
                ],
            ], __x('Validator', 'Allowed file extension is: .png, .jpeg, .jpg')); */

```

controller side method:-

> public function add()  
> {  
> $article = $this-\>Articles-\>newEmptyEntity();  
> if ($this-\>request-\>is(‘post’)) {  
> if ($this-\>request-\>getData(‘featureImage’)-\>getSize() \> 0) :  
> $article-\>feature\_image = ‘images’ . DS . ‘feature-image’ . DS . $this-\>Zira-\>f

```auto
ileUploads('feature-image', $this->request);
            endif;
            //dd($this->request->getData());
            $article = $this->Articles->patchEntity($article, $this->request->getData());
            $article->user_id = $this->request->getAttribute('identity')->getIdentifier();
            //dd($article);
            if ($this->Articles->save($article)) {
                $this->Flash->success(__('The article has been saved.'));

                return $this->redirect(['action' => 'index']);
            }
            $this->Flash->error(__('The article could not be saved. Please, try again.'));
        }
        $users = $this->Articles->Users->find('list', limit: 200)->all();
        $categorys = $this->Articles->Categorys->find('list', limit: 200)->all();
        $this->set(compact('article', 'users', 'categorys'));
    }

```

```auto
<?= $this->Form->create($article, ['type' => "file"]) ?>
<?= $this->Form->control('featureImage',['ttype'=>"file'])?>
<?= $this->Form->end()?>

```

Table Field Is feature\_image… but i am sending image through featureImage

[Next page](https://discourse.cakephp.org/t/cakephp-5-file-uploading-validation-mimetype-does-not-work/11856.md?page=2)
