<p>You can use the <a href="https://github.com/craftcms/docs/blob/master/en/updating-plugins.md#events" rel="nofollow noreferrer">Event</a> System from Yii/Craft and check for the file size during an upload</p>

<pre><code>Event::on(
    Asset::class,
    Element::EVENT_BEFORE_SAVE,
    function(ModelEvent $modelEvent){
        /** @var Asset $model */
        $model = $modelEvent-&gt;sender;
        $tmpPath = $model-&gt;tempFilePath;
        $fileSize = filesize($tmpPath);
        if($fileSize &gt; $someVariable){
            return false;
        }
    }
);
</code></pre>

<p>You can set <code>$someVariable</code> by a custom field in your volume <code>$volume = Craft::$app-&gt;volumes-&gt;getVolumeById($model-&gt;volumeId);</code> or by some config variables or in set of globals and so on</p>

CC BY-SA 3.0
2017-12-20T16:00:12.867Z
2017-12-20T16:00:12.867Z
Robin Schambach
<p>Craft has no settings defined for it. The only setting it has is <strong>maxUploadFileSize</strong> which you have already mentioned. So the solution of this is using <a href="https://github.com/aelvan/Imager-Craft" rel="nofollow noreferrer">Imager</a> Plugin. There is also a <a href="https://nystudio107.com/blog/creating-optimized-images-in-craft-cms" rel="nofollow noreferrer">blog</a> from <a href="https://nystudio107.com/" rel="nofollow noreferrer">nystudio107</a> on this. Let Client upload image of any size or dimension, but you could resize them using <a href="https://github.com/aelvan/Imager-Craft" rel="nofollow noreferrer">Imager</a> plugin.</p>

CC BY-SA 3.0
2017-12-20T15:25:51.067Z
2017-12-25T14:11:48.370Z
2017-12-25T14:11:48.370Z
hashtagerrors
<p>How can I set file size upload limits on a per-Asset Source basis?</p>

<p>I can only see how to set a global <a href="https://craftcms.com/docs/config-settings#maxUploadFileSize" rel="nofollow noreferrer">maxUploadFileSize</a> in the general config.</p>

<p>I'm sure I must be missing something obvious though, as this is something that was even possible in EE1! :-) It's a common use case to have a lower limit on some upload destinations to keep clients from uploading massive files (e.g. images), and to allow a higher limit on certain upload destinations for general file downloads.</p>

<p>Otherwise, we will have to use a 10mb limit for everything globally (as an example), and the clients can then upload images up to that size (which can happen with high resolution images). This will waste storage space and potentially run into PHP memory issues when resizing high resolution images for the front-end. :-/</p>

<assets><sources>
How to set different file size upload limits for different Asset Sources
# How to set different file size upload limits for different Asset Sources

(Asked by: username_0 on 2017-12-20)

How can I set file size upload limits on a per-Asset Source basis?

I can only see how to set a global [maxUploadFileSize](https://craftcms.com/docs/config-settings#maxUploadFileSize) in the general config.

I'm sure I must be missing something obvious though, as this is something that was even possible in EE1! :-) It's a common use case to have a lower limit on some upload destinations to keep clients from uploading massive files (e.g. images), and to allow a higher limit on certain upload destinations for general file downloads.

Otherwise, we will have to use a 10mb limit for everything globally (as an example), and the clients can then upload images up to that size (which can happen with high resolution images). This will waste storage space and potentially run into PHP memory issues when resizing high resolution images for the front-end. :-/

**Tags:** assets, sources

---

## 2 answers

### Answer by username_1 on 2017-12-20. Score: 3

You can use the Event System from Yii/Craft and check for the file size during an upload
Event::on(
    Asset::class,
    Element::EVENT_BEFORE_SAVE,
    function(ModelEvent $modelEvent){
        /** @var Asset $model */
        $model = $modelEvent->sender;
        $tmpPath = $model->tempFilePath;
        $fileSize = filesize($tmpPath);
        if($fileSize > $someVariable){
            return false;
        }
    }
);

You can set $someVariable by a custom field in your volume $volume = Craft::$app->volumes->getVolumeById($model->volumeId); or by some config variables or in set of globals and so on


---

### Answer by username_2 on 2017-12-20. Score: 2

Craft has no settings defined for it. The only setting it has is **maxUploadFileSize** which you have already mentioned. So the solution of this is using [Imager](https://github.com/aelvan/Imager-Craft) Plugin. There is also a [blog](https://nystudio107.com/blog/creating-optimized-images-in-craft-cms) from [nystudio107](https://nystudio107.com/) on this. Let Client upload image of any size or dimension, but you could resize them using [Imager](https://github.com/aelvan/Imager-Craft) plugin.


---

