craft()->on('entries.saveEntry', function(Event $event)
    // entry slug, will be used as image name
    $EntryTitle = $event->params['entry']['slug'];

    // Get attached image
    $entry = $event->params['entry']['imgdir'][0];

    craft()->assets->renameFile($entry, $EntryTitle);
    $entry->setAttribute('filename', $EntryTitle);


This init() code gets the current entry title right after it was saved, and is supposed to use it to rename an attached file. I used a method given by another developer in the context of a saveAsset, but it doesn't seem to work in this new context (→ see Change asset name on upload).

I'm not sure i've set the $entry variable to correct data. Fact is I'm able trough the path you see above, to get access to the asset. Please tell me where this code is wrong.

2 Answers 2


You probably just confused the entry model and the asset model, see if the code below works for you. I guess the entries.onBeforeSaveEntry event is a better match in this case, and I also added some conditionals to only do something for a specific entry section and type, and if there's exactly one asset selected. I hope this helps (code is untested)!

craft()->on('entries.beforeSaveEntry', function(Event $event)
    $entry = $event->params['entry'];

    if (($entry->section['handle'] == 'friends') && ($entry->type['handle'] == 'friends'))
        $title = $entry->slug;
        $assets = $entry->imgdir;

        if (count($assets) != 1)
            $event->params['entry']->addError('imgdir', Craft::t('Exactly 1 asset has to be selected.'));
            $event->performAction = false;
            $filename = $title.'.'.$assets[0]->getExtension();

            craft()->assets->renameFile($assets[0], $filename);
  • Thanks for your answer. I tried your code but it does not change the image filename, it's as if it does nothing particular.
    – Benj
    Jan 13, 2016 at 13:31
  • 1
    You forgot to add the file extension to the new title, as suggested in mmikkel's original answer and so did I, corrected it in the answer above.
    – carlcs
    Jan 13, 2016 at 14:50
  • Does this work with Amazon S3 too? My application is a bit different. Immediately after upload, I need to push the mp3 file to Soundcloud using their API. I have experience with their API. I just don't know how I access the file to send to them. Apr 12, 2016 at 14:28
  • @david yes, you can use the asset events to do your things with an external API. Probably worth adding a new question for this though.
    – carlcs
    Apr 12, 2016 at 15:11
  • What would be the equivalent in Craft 3? craftcms.stackexchange.com/a/27604/298 works, but I want the new file name to be a concatenation of Post Date (or upload time stamp) & the entry's author id. Oct 21, 2018 at 23:33

In Craft 3, here's a version that worked for me to change the file name after upload (in a module's init() function):

    function (ElementEvent $event) {
        $asset = $event->element;
        if ($event->isNew && $asset instanceof craft\elements\Asset) {
             $newFileName = "something.txt";
              Craft::$app->assets->moveAsset($asset,$asset->getFolder(),filter_var($asset->getFilename(), $newFileName);

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.