<p>You can check the <a href="https://docs.craftcms.com/api/v3/craft-web-uploadedfile.html#public-properties" rel="nofollow noreferrer"><code>size</code> property</a> on the <code>UploadedFile</code> instance.</p>
<pre class="lang-php prettyprint-override"><code>$limit = 2_097_152;
if ($uploadedFile-&gt;size &gt; $limit) {
    // abort and return an error message
}
</code></pre>

CC BY-SA 4.0
2024-06-20T14:27:42.587Z
And in my case, how can I display an error message (that the file is too heavy)? I have two checks planned before successfully adding (1. checking for the weight of the photo. 2. Unique name). There is a condition for the weight of the file, and a condition for checking the uniqueness of the name, too. But how do I show the error to the user? How to return an error message?
Dimi
CC BY-SA 4.0
2024-06-20T20:09:51.840Z
By the way, your solution doesn't work for some reason. I set $limit = 524288; But the file weighing 1.5 megabytes was uploaded to the family-photos directory anyway
Dimi
CC BY-SA 4.0
2024-06-20T20:17:05.120Z
But no! It's okay! The condition works. I just didn't understand why you put "_" in the limit
Dimi
CC BY-SA 4.0
2024-06-20T20:30:41.577Z
I found such a construction in the documentation: if (!$success) { Craft::error('The file size is too large!', __METHOD__); } 
But for some reason, an error message is not displayed on the site in case of a large file weight. Is there something you need to specify in the html form?
Dimi
CC BY-SA 4.0
2024-06-20T22:31:02.947Z
I believe that this should be inserted into the template: {% macro errorList(errors) %}{% if errors %}{{ ul(errors, {class: 'errors'}) }}{% endif %}{% endmacro %}
I tried to do this, but it does not output an error
Dimi
CC BY-SA 4.0
2024-06-21T09:18:33.860Z
i create a new question with my problem with error messages: https://craftcms.stackexchange.com/questions/41808/how-can-i-display-a-message-to-the-user-that-the-field-is-filled-in-incorrectly
Dimi
CC BY-SA 4.0
2024-06-21T13:35:37.530Z
@Dimi The underscore is just a numeric separator to improve readability. See https://php.watch/versions/7.4/underscore_numeric_separator
MoritzLost
CC BY-SA 4.0
2024-06-21T13:37:08.620Z
@Dimi `Craft::error()` will log errors to the logs, but not output anything on the page. For this, you need to add an error on the model you're creating (in this case the `$family` entry), then pass that model back to the template. Then use `family.getErrors()` in Twig to show all the errors to the user.
MoritzLost
CC BY-SA 4.0
2024-06-21T14:27:18.243Z
I have created a separate issue about error messages. Could you write a solution there. I just didn't quite understand where to add what. The example is easier to remember. The question is here: https://craftcms.stackexchange.com/questions/41808/how-can-i-display-a-message-to-the-user-that-the-field-is-filled-in-incorrectly
Dimi
CC BY-SA 4.0
2024-06-21T20:06:40.533Z
need easy example)
Dimi
CC BY-SA 4.0
2024-06-19T12:17:52.013Z
2024-06-19T12:17:52.013Z
MoritzLost
<p>I have a simple form on the website:</p>
<pre><code>&lt;form method=&quot;POST&quot; enctype=&quot;multipart/form-data&quot;&gt;
{{ csrfInput() }}
{{ actionInput('families/families/start') }}

&lt;input type=&quot;file&quot; name=&quot;img&quot; value=&quot;&quot;&gt;

&lt;input type=&quot;submit&quot; value=&quot;go&quot;&gt;
&lt;/form&gt;
</code></pre>
<p>And there is a controller:</p>
<pre><code>&lt;?php
namespace modules\families\controllers;

use Craft;
use craft\web\Controller;
use craft\elements\Asset;
use craft\helpers\Assets;
use craft\web\UploadedFile;
use craft\elements\Entry;

class FamiliesController extends Controller
{

    protected array|bool|int $allowAnonymous = true;

    public function actionStart() {

        $uploadedFile = UploadedFile::getInstanceByName('img');
        $tempPath = $uploadedFile-&gt;saveAsTempFile();

        $volume = Craft::$app-&gt;getVolumes()-&gt;getVolumeByHandle('uploads');
        $folder = Craft::$app-&gt;getAssets()-&gt;ensureFolderByFullPathAndVolume('family-photos', $volume, false);

        $asset = new Asset();
        $asset-&gt;newFolderId = $folder-&gt;id;
        $asset-&gt;volumeId = $volume-&gt;id;
        $asset-&gt;tempFilePath = $tempPath;
        $asset-&gt;setFilename(Assets::prepareAssetName($uploadedFile-&gt;name)); 
        $asset-&gt;avoidFilenameConflicts = true;
        $asset-&gt;setScenario(Asset::SCENARIO_CREATE);

        Craft::$app-&gt;elements-&gt;saveElement($asset);


    }
}
</code></pre>
<p>How can I check the uploaded file for its weight and notify the user that his file is too large? I would not like to allow users to upload files that weigh more than 2 megabytes</p>

<craft3><assets><form><upload>
How can I check the size of the file uploaded via the form?
# How can I check the size of the file uploaded via the form?

(Asked by: username_0 on 2024-06-19)

I have a simple form on the website:

{{ csrfInput() }}
{{ actionInput('families/families/start') }}






And there is a controller:
saveAsTempFile();

        $volume = Craft::$app->getVolumes()->getVolumeByHandle('uploads');
        $folder = Craft::$app->getAssets()->ensureFolderByFullPathAndVolume('family-photos', $volume, false);

        $asset = new Asset();
        $asset->newFolderId = $folder->id;
        $asset->volumeId = $volume->id;
        $asset->tempFilePath = $tempPath;
        $asset->setFilename(Assets::prepareAssetName($uploadedFile->name)); 
        $asset->avoidFilenameConflicts = true;
        $asset->setScenario(Asset::SCENARIO_CREATE);

        Craft::$app->elements->saveElement($asset);


    }
}

How can I check the uploaded file for its weight and notify the user that his file is too large? I would not like to allow users to upload files that weigh more than 2 megabytes

**Tags:** craft3, assets, form, upload

---

## 1 answers

### Answer by username_1 on 2024-06-19. Score: 1

You can check the size property on the UploadedFile instance.
$limit = 2_097_152;
if ($uploadedFile->size > $limit) {
    // abort and return an error message
}

> **Comments:**
> **username_0**: And in my case, how can I display an error message (that the file is too heavy)? I have two checks planned before successfully adding (1. checking for the weight of the photo. 2. Unique name). There is a condition for the weight of the file, and a condition for checking the uniqueness of the name, too. But how do I show the error to the user? How to return an error message?  
> **username_0**: By the way, your solution doesn't work for some reason. I set $limit = 524288; But the file weighing 1.5 megabytes was uploaded to the family-photos directory anyway  
> **username_0**: But no! It's okay! The condition works. I just didn't understand why you put "_" in the limit  
> **username_0**: I found such a construction in the documentation: if (!$success) { Craft::error('The file size is too large!', __METHOD__); }  But for some reason, an error message is not displayed on the site in case of a large file weight. Is there something you need to specify in the html form?  
> **username_0**: I believe that this should be inserted into the template: {% macro errorList(errors) %}{% if errors %}{{ ul(errors, {class: 'errors'}) }}{% endif %}{% endmacro %} I tried to do this, but it does not output an error  
> **username_0**: i create a new question with my problem with error messages: https://craftcms.stackexchange.com/questions/41808/how-can-i-display-a-message-to-the-user-that-the-field-is-filled-in-incorrectly  
> **username_1**: @Dimi The underscore is just a numeric separator to improve readability. See https://php.watch/versions/7.4/underscore_numeric_separator  
> **username_1**: @Dimi `Craft::error()` will log errors to the logs, but not output anything on the page. For this, you need to add an error on the model you're creating (in this case the `$family` entry), then pass that model back to the template. Then use `family.getErrors()` in Twig to show all the errors to the user.  
> **username_0**: I have created a separate issue about error messages. Could you write a solution there. I just didn't quite understand where to add what. The example is easier to remember. The question is here: https://craftcms.stackexchange.com/questions/41808/how-can-i-display-a-message-to-the-user-that-the-field-is-filled-in-incorrectly  
> **username_0**: need easy example)  

---

