<p>You are mixing <code>Craft 2</code> methods with <code>Craft 3</code> methods.
This will get the the assets extension with <code>$asset-&gt;getFilename(true)</code> and add <code>test_</code> in the beginning of the filename:</p>

<pre><code>Event::on(
    \craft\services\Elements::class,
    \craft\services\Elements::EVENT_AFTER_SAVE_ELEMENT,
    function(Event $event)   {
        $asset = $event-&gt;element;
        $folderId = $asset-&gt;getFolder();


        if ($event-&gt;isNew &amp;&amp; $asset instanceof craft\elements\Asset &amp;&amp; $folderId == '22') {
            $newName =  'test_' . $asset-&gt;getFilename(true);
            Craft::$app-&gt;assets-&gt;moveAsset($asset, $folderId, $newName);
        }
    }
);
</code></pre>

CC BY-SA 4.0
2019-06-24T19:11:57.180Z
2019-06-24T19:17:30.067Z
2019-06-24T19:17:30.067Z
HendrikEng
<p>Thank you very much, it works almost I would like to.</p>

<p>Actually, the function of getFolder() gets the folder as a string.
So I modified the function like this;</p>

<pre><code>Event::on(\craft\services\Elements::class, \craft\services\Elements::EVENT_AFTER_SAVE_ELEMENT, function(Event $event) {
    if ($event-&gt;element instanceof \craft\elements\Asset) {
        $asset = $event-&gt;element;
        $folder = $asset-&gt;getFolder();
        $folderId = $folder-&gt;id;

        if ($event-&gt;isNew &amp;&amp; $folderId == '22') {
            $newName =  'test_' . $folderId . '_' . $asset-&gt;getFilename(true);
            Craft::$app-&gt;assets-&gt;moveAsset($asset, $folder, $newName);
        }
    }
});
</code></pre>

CC BY-SA 4.0
2019-06-24T19:39:29.263Z
2019-06-25T14:17:29.780Z
2019-06-25T14:17:29.780Z
Kota.M
<p>I would like to rename new assets by PHP, but somehow it does not work.
In Craft 3, I suppose it is like;</p>

<pre><code>Event::on(\craft\services\Elements::class, \craft\services\Elements::EVENT_AFTER_SAVE_ELEMENT, function(Event $event) {
    if ($event-&gt;params['isNewAsset']) {
        $asset = $event-&gt;params['asset'];
        $folder_id = $asset-&gt;folderId;

        if ($folder_id == "22")
        {
            $filename = $asset-&gt;filename . "_test";
            craft()-&gt;assets-&gt;renameFile($asset, $filename);
        }
    }
});
</code></pre>

<p>My code is like that, but this message appears;</p>

<pre><code>Upload failed. The error message was: “Getting unknown property: craft\events\ElementEvent::params
</code></pre>

<p>Could you help me?</p>

<craft3><assets><php>
Rename Assets when it is uploaded
# Rename Assets when it is uploaded

(Asked by: username_0 on 2019-06-24)

I would like to rename new assets by PHP, but somehow it does not work.
In Craft 3, I suppose it is like;
Event::on(\craft\services\Elements::class, \craft\services\Elements::EVENT_AFTER_SAVE_ELEMENT, function(Event $event) {
    if ($event->params['isNewAsset']) {
        $asset = $event->params['asset'];
        $folder_id = $asset->folderId;

        if ($folder_id == "22")
        {
            $filename = $asset->filename . "_test";
            craft()->assets->renameFile($asset, $filename);
        }
    }
});

My code is like that, but this message appears;
Upload failed. The error message was: “Getting unknown property: craft\events\ElementEvent::params

Could you help me?

**Tags:** craft3, assets, php


---

## 2 answers

### Answer by username_1 on 2019-06-24. Score: 2 | Accepted answer

You are mixing Craft 2 methods with Craft 3 methods.
This will get the the assets extension with $asset->getFilename(true) and add test_ in the beginning of the filename:
Event::on(
    \craft\services\Elements::class,
    \craft\services\Elements::EVENT_AFTER_SAVE_ELEMENT,
    function(Event $event)   {
        $asset = $event->element;
        $folderId = $asset->getFolder();


        if ($event->isNew && $asset instanceof craft\elements\Asset && $folderId == '22') {
            $newName =  'test_' . $asset->getFilename(true);
            Craft::$app->assets->moveAsset($asset, $folderId, $newName);
        }
    }
);


---

### Answer by username_0 on 2019-06-24. Score: 1

Thank you very much, it works almost I would like to.
Actually, the function of getFolder() gets the folder as a string.
So I modified the function like this;
Event::on(\craft\services\Elements::class, \craft\services\Elements::EVENT_AFTER_SAVE_ELEMENT, function(Event $event) {
    if ($event->element instanceof \craft\elements\Asset) {
        $asset = $event->element;
        $folder = $asset->getFolder();
        $folderId = $folder->id;

        if ($event->isNew && $folderId == '22') {
            $newName =  'test_' . $folderId . '_' . $asset->getFilename(true);
            Craft::$app->assets->moveAsset($asset, $folder, $newName);
        }
    }
});


---

