# 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:** 8\
**Page:** 2

<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, 4:12pm UTC](https://discourse.cakephp.org/t/cakephp-5-file-uploading-validation-mimetype-does-not-work/11856/21 "2024-02-09T16:12:23Z")

</div>

Try this:

```auto
public function add() {
    $article = $this->Articles->newEmptyEntity();
    if ($this->request->is('post')) {
        $article = $this->Articles->patchEntity($article, $this->request->getData());
        $article->user_id = $this->request->getAttribute('identity')->getIdentifier();
        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'));
}

```

You change the value of the `feature_image` variable before the `patchEntity`, so it can’t validate.

And use this validator

```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 9, 2024, 4:13pm UTC](https://discourse.cakephp.org/t/cakephp-5-file-uploading-validation-mimetype-does-not-work/11856/22 "2024-02-09T16:13:04Z")

</div>

that is here before save data… and even you can check featureImage file object

 ![Screenshot from 2024-02-09 21-38-38](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/b/bca8564fd10708c41948e464ca2d1d38ff0e16ee.png)

and here you can see in feature\_image field… please look at the extension (webp)… file upload from client and it does not throw error but it must have throw error here because in validation i just allow only JPG JPEG PNG pictures… Me also little confused bro @shadowx.jb and i really thanks you still helping me

 ![Screenshot from 2024-02-09 21-40-25](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/f/f886627f3c9cb6efc4dd70de4e3d666f2ef4a2b0.png)

---

<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:20pm UTC](https://discourse.cakephp.org/t/cakephp-5-file-uploading-validation-mimetype-does-not-work/11856/23 "2024-02-09T16:20:03Z")

</div>

this way i am getting error… of course it throw error if i send direct file object in PatchEntity method… so that why i am just setting featureImage field in from and during the save data i just fill the value of feature\_image with the image Url… that all handle by component

 ![Screenshot from 2024-02-09 21-46-25](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/8/8d29a736b83df2be8f191a747b13f74ac5294393.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, 4:34pm UTC](https://discourse.cakephp.org/t/cakephp-5-file-uploading-validation-mimetype-does-not-work/11856/24 "2024-02-09T16:34:52Z")

</div>

I try it and its works…

Entity

```auto
class Article extends Entity
{
    protected $_accessible = [
        'article_title' => true,
        'article_content' => true,
        'user_id' => true,
    ];
}

```

Table

```auto
public function validationDefault(Validator $validator): Validator
{
    $validator
        ->scalar('article_title')
        ->requirePresence('article_title', 'create')
        ->notEmptyString('article_title');

    $validator
        ->scalar('article_content')
        ->requirePresence('article_content', 'create')
        ->notEmptyString('article_content');

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

    return $validator;
}

```

Template

```auto
echo $this->Form->create($article, ['type' => 'file']);
echo $this->Form->control('user_id', [
    'value' => 1,
]);
echo $this->Form->control('article_title');
echo $this->Form->control('article_content');
echo $this->Form->control('featureImage', [
    'type' => 'file',
]);
echo $this->Form->end();

```

Controller

```auto
$article = $this->Article->newEmptyEntity();
if ($this->request->is('post')) {
    $article = $this->Article->patchEntity($article, $this->request->getData());
    if ($this->Article->save($article)) {
        $this->Flash->success('Success');

        return $this->redirect(['action' => 'index']);
    }
    $this->Flash->error('error');
}
$this->set(compact('article'));

```

---

<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 11, 2024, 12:54pm UTC](https://discourse.cakephp.org/t/cakephp-5-file-uploading-validation-mimetype-does-not-work/11856/25 "2024-02-11T12:54:14Z")

</div>

Hi @shadowx.jb now i followed same steps as you mention above but now i am getting this :-

 ![Screenshot from 2024-02-11 18-22-41](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/a/a8e20be5f8c7fab264ca3170409f1820a625d78f.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 11, 2024, 2:31pm UTC](https://discourse.cakephp.org/t/cakephp-5-file-uploading-validation-mimetype-does-not-work/11856/26 "2024-02-11T14:31:02Z")

</div>

you seem to be trying to save a uploaded file into a string column in the database.

This means validation has passed and the Cake ORM tried to convert the submitted data into something the database can understand (in your example its the `StringType` because your column is probably a varchar or something like that)

---

<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 15, 2024, 3:45pm UTC](https://discourse.cakephp.org/t/cakephp-5-file-uploading-validation-mimetype-does-not-work/11856/27 "2024-02-15T15:45:52Z")

</div>

so now problem solved 🤣. Actually there is some logical and common scenes error 😆 but now all are solved my code is working fine…

thank to all guys @shadowx.jb @KevinPfeifer @Zuluru

 ![Screenshot from 2024-02-15 21-07-37](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/6/6a3707d8e3445b4d6de287cf829ec948902e0043.png)

---

<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 15, 2024, 3:47pm UTC](https://discourse.cakephp.org/t/cakephp-5-file-uploading-validation-mimetype-does-not-work/11856/28 "2024-02-15T15:47:06Z")

</div>

Hi… @shadowx.jb now my code work fine… i just change my controller code of flow then all problems solved

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