<p>Well, after searching for this for more than 2 hours, I bumped into a working solution;</p>

<pre><code>Event::on(
    Elements::class,
    Elements::EVENT_AFTER_SAVE_ELEMENT,
    function(ElementEvent $event)   {

        $asset = $event-&gt;element;

        if ($event-&gt;isNew &amp;&amp; $asset instanceof craft\elements\Asset) {
            $newFileName = 'file-'.md5(time()).'.'.$asset-&gt;getExtension();
            Craft::$app-&gt;assets-&gt;moveAsset($asset, $asset-&gt;getFolder(), $newFileName);
        }
    }
);
</code></pre>

<p>In case somebody needs it!</p>

CC BY-SA 4.0
2018-11-14T11:21:57.287Z
Lifesaver! But this didn't work 100% for me, so I changed `function(ElementEvent $event)` to `function(Event $event)` and it worked perfectly :)
Jason Mayo
CC BY-SA 4.0
2018-10-30T14:02:52.343Z
2018-10-30T14:02:52.343Z
Arjan
<p>I found that the other answer resulted in a warning about a duplicate file in my case (I want to lowercase the filenames and the system is not case sensitive).</p>
<p>Here's what I found worked – <strong>there could be problems with this code!!</strong></p>
<pre class="lang-php prettyprint-override"><code>if (Craft::$app-&gt;getRequest()-&gt;getIsCpRequest()) {
    /**
     * Lowercase the filename just before save.
     * This is done so the filename is lowercase when it's initially saved.
     */
    Event::on(
        Asset::class,
        Element::EVENT_BEFORE_SAVE,
        function (ModelEvent $event): void {
            /** @var Asset $asset */
            $asset = $event-&gt;sender;

            if ($asset-&gt;firstSave) {
                $newFilename = strtolower($asset-&gt;getFilename());
                if ($newFilename !== $asset-&gt;getFilename()) {
                    $asset-&gt;setFilename($newFilename);
                }
            }
        },
    );

    /**
     * After save, move the file to the right location with the right filename.
     * If we don't do this, Craft will see it as a duplicate filename.
     */
    Event::on(
        Asset::class,
        Element::EVENT_AFTER_SAVE,
        function (ModelEvent $event): void {
            /** @var Asset $asset */
            $asset = $event-&gt;sender;

            if ($event-&gt;isNew) {
                $newFilename = strtolower($asset-&gt;getFilename());
                if ($newFilename !== $asset-&gt;getFilename()) {
                    Craft::$app-&gt;assets-&gt;moveAsset(
                        $asset,
                        $asset-&gt;getFolder(),
                        $newFilename,
                    );
                }
            }
        },
    );
}
</code></pre>

CC BY-SA 4.0
2022-09-29T04:14:31.213Z
2022-09-29T04:14:31.213Z
Darryl Hein
<p>I just posted a very nice solution in another S/E thread:</p>
<p><a href="https://craftcms.stackexchange.com/a/40636/6886">https://craftcms.stackexchange.com/a/40636/6886</a></p>
<p>Please have a look at it there and like it if you want.</p>

CC BY-SA 4.0
2023-03-21T13:52:38.230Z
2023-03-21T13:52:38.230Z
Matthias Redl-Mann
<p>I want to change the filename before the file is stored, to prevent ugly, long and weird filenames uploaded by users. </p>

<p>I've found an example for Craft 2, but because of the changes for events I can't rebuild this for Craft 3 (<a href="https://craftcms.stackexchange.com/questions/13154/change-asset-name-on-upload">Change asset name on upload</a>)</p>

<p>I also tried to change it a event, no luck;</p>

<pre><code>Event::on(
    Asset::class,
    Asset::EVENT_BEFORE_HANDLE_FILE,
    function(AssetEvent $event) {

        $asset = $event-&gt;asset;                         
        $filename = 'filename'.md5(time()).'.'.$asset-&gt;getExtension();
        $asset-&gt;filename = $filename;
    }
);
</code></pre>

<p>Apparently the filename property is read-only, so you can only change properties like the title.</p>

<p>Any help is appreciated!</p>

<assets><craft3><upload>
How to rename filename of asset before (or after) upload in Craft 3
# How to rename filename of asset before (or after) upload in Craft 3

(Asked by: username_0 on 2018-10-30)

I want to change the filename before the file is stored, to prevent ugly, long and weird filenames uploaded by users. 
I've found an example for Craft 2, but because of the changes for events I can't rebuild this for Craft 3 (Change asset name on upload)
I also tried to change it a event, no luck;
Event::on(
    Asset::class,
    Asset::EVENT_BEFORE_HANDLE_FILE,
    function(AssetEvent $event) {

        $asset = $event->asset;                         
        $filename = 'filename'.md5(time()).'.'.$asset->getExtension();
        $asset->filename = $filename;
    }
);

Apparently the filename property is read-only, so you can only change properties like the title.
Any help is appreciated!

**Tags:** assets, craft3, upload

---

## 3 answers

### Answer by username_0 on 2018-10-30. Score: 6

Well, after searching for this for more than 2 hours, I bumped into a working solution;
Event::on(
    Elements::class,
    Elements::EVENT_AFTER_SAVE_ELEMENT,
    function(ElementEvent $event)   {

        $asset = $event->element;

        if ($event->isNew && $asset instanceof craft\elements\Asset) {
            $newFileName = 'file-'.md5(time()).'.'.$asset->getExtension();
            Craft::$app->assets->moveAsset($asset, $asset->getFolder(), $newFileName);
        }
    }
);

In case somebody needs it!

> **Comments:**
> **username_1**: Lifesaver! But this didn't work 100% for me, so I changed `function(ElementEvent $event)` to `function(Event $event)` and it worked perfectly :)  

---

### Answer by username_2 on 2022-09-29. Score: 1

I found that the other answer resulted in a warning about a duplicate file in my case (I want to lowercase the filenames and the system is not case sensitive).
Here's what I found worked – there could be problems with this code!!
if (Craft::$app->getRequest()->getIsCpRequest()) {
    /**
     * Lowercase the filename just before save.
     * This is done so the filename is lowercase when it's initially saved.
     */
    Event::on(
        Asset::class,
        Element::EVENT_BEFORE_SAVE,
        function (ModelEvent $event): void {
            /** @var Asset $asset */
            $asset = $event->sender;

            if ($asset->firstSave) {
                $newFilename = strtolower($asset->getFilename());
                if ($newFilename !== $asset->getFilename()) {
                    $asset->setFilename($newFilename);
                }
            }
        },
    );

    /**
     * After save, move the file to the right location with the right filename.
     * If we don't do this, Craft will see it as a duplicate filename.
     */
    Event::on(
        Asset::class,
        Element::EVENT_AFTER_SAVE,
        function (ModelEvent $event): void {
            /** @var Asset $asset */
            $asset = $event->sender;

            if ($event->isNew) {
                $newFilename = strtolower($asset->getFilename());
                if ($newFilename !== $asset->getFilename()) {
                    Craft::$app->assets->moveAsset(
                        $asset,
                        $asset->getFolder(),
                        $newFilename,
                    );
                }
            }
        },
    );
}


---

### Answer by username_3 on 2023-03-21. Score: 1

I just posted a very nice solution in another S/E thread:

<https://craftcms.stackexchange.com/a/40636/6886>

Please have a look at it there and like it if you want.


---

