Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-06-20T14:27:42.587Z
Answers.Text: 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?
Answers.UserDisplayName: Dimi
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-06-20T20:09:51.840Z
Answers.Text: 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
Answers.UserDisplayName: Dimi
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-06-20T20:17:05.120Z
Answers.Text: But no! It's okay! The condition works. I just didn't understand why you put "_" in the limit
Answers.UserDisplayName: Dimi
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-06-20T20:30:41.577Z
Answers.Text: 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?
Answers.UserDisplayName: Dimi
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-06-20T22:31:02.947Z
Answers.Text: 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
Answers.UserDisplayName: Dimi
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-06-21T09:18:33.860Z
Answers.Text: 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
Answers.UserDisplayName: Dimi
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-06-21T13:35:37.530Z
Answers.Text: @Dimi The underscore is just a numeric separator to improve readability. See https://php.watch/versions/7.4/underscore_numeric_separator
Answers.UserDisplayName: MoritzLost
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-06-21T13:37:08.620Z
Answers.Text: @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.
Answers.UserDisplayName: MoritzLost
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-06-21T14:27:18.243Z
Answers.Text: 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
Answers.UserDisplayName: Dimi
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-06-21T20:06:40.533Z
Answers.Text: need easy example)
Answers.UserDisplayName: Dimi
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-06-19T12:17:52.013Z
Answers.LastActivityDate: 2024-06-19T12:17:52.013Z
Answers.UserDisplayName: MoritzLost
Body: <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>
  
Tags: <craft3><assets><form><upload>
Title: How can I check the size of the file uploaded via the form?
ThreadText: # 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:
  
  ```
  <form method="POST" enctype="multipart/form-data">
  {{ csrfInput() }}
  {{ actionInput('families/families/start') }}
  
  <input type="file" name="img" value="">
  
  <input type="submit" value="go">
  </form>
  
  ```
  
  And there is a controller:
  
  ```
  <?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->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](https://docs.craftcms.com/api/v3/craft-web-uploadedfile.html#public-properties) on the `UploadedFile` instance.
  
  ```php
  $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**: @username_0 The underscore is just a numeric separator to improve readability. See https://php.watch/versions/7.4/underscore_numeric_separator  
  > **username_1**: @username_0 `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)  
  
  ---
  
  