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">

And there is a controller:

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->avoidFilenameConflicts = true;



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

1 Answer 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
  • 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
    Commented Jun 20 at 14:27
  • 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
    Commented Jun 20 at 20:09
  • But no! It's okay! The condition works. I just didn't understand why you put "_" in the limit
    – Dimi
    Commented Jun 20 at 20:17
  • 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
    Commented Jun 20 at 20:30
  • 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
    Commented Jun 20 at 22:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.