4

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!

0

3 Answers 3

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!

1
  • 1
    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
    Commented Nov 14, 2018 at 11:21
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,
                    );
                }
            }
        },
    );
}
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.

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.