# Validate uploaded file size through model

**URL:** <https://discourse.cakephp.org/t/validate-uploaded-file-size-through-model/6755>\
**Category:** Need Help\
**Created:** [October 14, 2019, 8:20am UTC](https://discourse.cakephp.org/t/validate-uploaded-file-size-through-model/6755 "2019-10-14T08:20:54Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![sneha](https://avatars.discourse-cdn.com/v4/letter/s/ecc23a/32.png) [@sneha](https://discourse.cakephp.org/u/sneha)\
**Post date:** [October 14, 2019, 8:20am UTC](https://discourse.cakephp.org/t/validate-uploaded-file-size-through-model/6755/1 "2019-10-14T08:20:54Z")

</div>

Hello,

I want to validate my uploaded file (image/doc/pdf/xls etc) for file size through model. The uploaded file should be less than 2 mb and document type can be either jpeg or pdf. Can anyone please help me through this . I am really struggling through this.

---

<div class="post-metadata">

**Author:** ![fernaog](https://avatars.discourse-cdn.com/v4/letter/f/c6cbf5/32.png) [@fernaog](https://discourse.cakephp.org/u/fernaog)\
**Post date:** [October 14, 2019, 5:11pm UTC](https://discourse.cakephp.org/t/validate-uploaded-file-size-through-model/6755/2 "2019-10-14T17:11:22Z")

</div>

Hello,

You can use the following plugin that already includes, among other things, file size validation:

> **[FriendsOfCake/cakephp-upload](https://github.com/FriendsOfCake/cakephp-upload)**
>
> CakePHP: Handle file uploading sans ridiculous automagic - FriendsOfCake/cakephp-upload

---

<div class="post-metadata">

**Author:** ![Graziel](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Graziel](https://discourse.cakephp.org/u/Graziel)\
**Post date:** [October 14, 2019, 6:18pm UTC](https://discourse.cakephp.org/t/validate-uploaded-file-size-through-model/6755/3 "2019-10-14T18:18:35Z")

</div>

there is built in validation for it

[https://api.cakephp.org/3.8/class-Cake.Validation.Validation.html#\_uploadedFile](https://api.cakephp.org/3.8/class-Cake.Validation.Validation.html#_uploadedFile)

---

<div class="post-metadata">

**Author:** ![sneha](https://avatars.discourse-cdn.com/v4/letter/s/ecc23a/32.png) [@sneha](https://discourse.cakephp.org/u/sneha)\
**Post date:** [October 15, 2019, 4:55am UTC](https://discourse.cakephp.org/t/validate-uploaded-file-size-through-model/6755/4 "2019-10-15T04:55:37Z")

</div>

Graziel, Can you please provide me with the syntax . It would be easier for me. Thanx in advance.

---

<div class="post-metadata">

**Author:** ![sneha](https://avatars.discourse-cdn.com/v4/letter/s/ecc23a/32.png) [@sneha](https://discourse.cakephp.org/u/sneha)\
**Post date:** [October 15, 2019, 12:36pm UTC](https://discourse.cakephp.org/t/validate-uploaded-file-size-through-model/6755/5 "2019-10-15T12:36:32Z")

</div>

$validator  
-\>requirePresence(‘document’, ‘create’)  
-\>notEmptyString(‘document’)  
-\>add(‘document’, [

```
			['rule' => ['extension',['pdf']], // default ['gif', 'jpeg', 'png', 'jpg']
                'message' => __('Only pdf files are allowed.')],
				['rule' => ['fileSize', '<=', '1024'], 
                'message' => __('Image must be less than 1MB')]
            ]
			
		  );

```

This is my validator, It provides validation for extension but not for image size.Can you please help me through this.

---

<div class="post-metadata">

**Author:** ![Graziel](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Graziel](https://discourse.cakephp.org/u/Graziel)\
**Post date:** [October 15, 2019, 1:06pm UTC](https://discourse.cakephp.org/t/validate-uploaded-file-size-through-model/6755/6 "2019-10-15T13:06:29Z")

</div>

i dont have currently any app with standard file upload so code can be a bit wrong but:

```auto
->uploadedFile('document', [
                'types' => [
                    'application/pdf', 
                    'images/jpeg',
                ],
                'maxSize' => 2 * 1024 * 1024
            ])

```

---

<div class="post-metadata">

**Author:** ![Schlaefer](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/schlaefer/32/1001_2.png) [@Schlaefer](https://discourse.cakephp.org/u/Schlaefer)\
**Post date:** [October 16, 2019, 5:59am UTC](https://discourse.cakephp.org/t/validate-uploaded-file-size-through-model/6755/7 "2019-10-16T05:59:58Z")

</div>

The validator setup looks OK.

> [@sneha](#):
>
> [‘fileSize’, ‘\<=’, ‘1024’],

means smaller than 1KB not 1 MB. Use e.g.

```
['fileSize', '<=', '1 MB'],

```

---

<div class="post-metadata">

**Author:** ![sneha](https://avatars.discourse-cdn.com/v4/letter/s/ecc23a/32.png) [@sneha](https://discourse.cakephp.org/u/sneha)\
**Post date:** [October 16, 2019, 6:55am UTC](https://discourse.cakephp.org/t/validate-uploaded-file-size-through-model/6755/8 "2019-10-16T06:55:54Z")

</div>

Yes it does. But it throws error in case of fileSize() as:  
filesize(): stat failed for c:\xampp\tmp\php3f06.tmp [core\src\validation\validation.php, line 1297.

Is there some issue, if you can trace ?

---

<div class="post-metadata">

**Author:** ![Schlaefer](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/schlaefer/32/1001_2.png) [@Schlaefer](https://discourse.cakephp.org/u/Schlaefer)\
**Post date:** [October 16, 2019, 7:18am UTC](https://discourse.cakephp.org/t/validate-uploaded-file-size-through-model/6755/9 "2019-10-16T07:18:31Z")

</div>

That usually means that PHP can’t access the file, which seems curious if mime-type checking works. But I have no clue about PHP on Windows.

Do you move away the file manually before the validator can pick it up? I would go to said line, pause the execution there and check that the tmp file does actually exist.

---

<div class="post-metadata">

**Author:** ![sneha](https://avatars.discourse-cdn.com/v4/letter/s/ecc23a/32.png) [@sneha](https://discourse.cakephp.org/u/sneha)\
**Post date:** [October 16, 2019, 10:23am UTC](https://discourse.cakephp.org/t/validate-uploaded-file-size-through-model/6755/10 "2019-10-16T10:23:00Z")

</div>

Thanks for help. It works fine now.

Earlier, I moved my file before reaching the validator. Thanks for guiding.

---

<div class="post-metadata">

**Author:** ![jehangir](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@jehangir](https://discourse.cakephp.org/u/jehangir)\
**Post date:** [May 3, 2020, 7:24am UTC](https://discourse.cakephp.org/t/validate-uploaded-file-size-through-model/6755/11 "2020-05-03T07:24:08Z")

</div>

cakephp 4.x: use separate -\>add for every rule like this -\>add(‘filed\_name’,‘rule\_name’,[  
‘rule’ =\> [‘rule\_name’, //parameters])
