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