<p>Just tested your code and it worked as you'd expect if you leave off the extra <code>getExtension()</code> call (the <code>filename</code> property already includes the extension).</p>

<pre><code>$filename = strtolower($asset-&gt;filename);
</code></pre>

<p>If it's not saving for you with that, you've got some other error happening elsewhere.</p>

CC BY-SA 3.0
2017-12-01T22:38:17.927Z
2017-12-01T22:38:17.927Z
Brad Bell
<p>As there is no native config to control the naming of assets I am trying to write a small plugin to do this.
Here is what I have;</p>

<pre><code>    craft()-&gt;on('assets.saveAsset', function (Event $event) {
        if ($event-&gt;params['isNewAsset']) {

            $asset = $event-&gt;params['asset'];

            // Set the filename to a variable, appending the file extension
            // $extension = $asset-&gt;getExtension();
            // $filename = strtolower($asset-&gt;filename.md5(time()).'.'.$asset-&gt;getExtension());
            $filename = strtolower($asset-&gt;filename.'.'.$asset-&gt;getExtension());

            // Rename the file on the server
            craft()-&gt;assets-&gt;renameFile($asset, $filename);

            // Set filename attribute and re-save asset
            $asset-&gt;setAttribute('filename', $filename);
            craft()-&gt;assets-&gt;storeFile($asset);
        }
    });
</code></pre>

<p>However, this currently results in a duplication of the extension like so;
model-rendering-image.jpg.jpg<br>
special-services.jpg.jpg  </p>

<p>If I remove this part of the code 
<code>'.'.$asset-&gt;getExtension())</code> then the asset isn't renamed at all.</p>

<p>Can't seem to get past this hurdle.</p>

<p>Anyone able to shed some light?</p>

<p>Also is there a reason that assets are not renamed to lowercase by default or why this isn't rolled in as a config option?</p>

CC BY-SA 3.0
2017-12-01T17:27:47.590Z
Possible duplicate of [Change asset name on upload](https://craftcms.stackexchange.com/questions/13154/change-asset-name-on-upload)
Brad Bell
CC BY-SA 3.0
2017-12-01T20:56:21.160Z
This is certainly based on that but my question is more specifically about how to remove the duplicate extension in the name.
Terry Upton
<plugin-development><assets>
Rename assets to lowercase
# Rename assets to lowercase

(Asked by: username_0 on 2017-11-30)

As there is no native config to control the naming of assets I am trying to write a small plugin to do this.
Here is what I have;
    craft()->on('assets.saveAsset', function (Event $event) {
        if ($event->params['isNewAsset']) {

            $asset = $event->params['asset'];

            // Set the filename to a variable, appending the file extension
            // $extension = $asset->getExtension();
            // $filename = strtolower($asset->filename.md5(time()).'.'.$asset->getExtension());
            $filename = strtolower($asset->filename.'.'.$asset->getExtension());

            // Rename the file on the server
            craft()->assets->renameFile($asset, $filename);

            // Set filename attribute and re-save asset
            $asset->setAttribute('filename', $filename);
            craft()->assets->storeFile($asset);
        }
    });

However, this currently results in a duplication of the extension like so;
model-rendering-image.jpg.jpg
special-services.jpg.jpg  
If I remove this part of the code 
'.'.$asset->getExtension()) then the asset isn't renamed at all.
Can't seem to get past this hurdle.
Anyone able to shed some light?
Also is there a reason that assets are not renamed to lowercase by default or why this isn't rolled in as a config option?

**Tags:** plugin-development, assets

> **Comments:**
> **username_1**: Possible duplicate of [Change asset name on upload](https://craftcms.stackexchange.com/questions/13154/change-asset-name-on-upload)  
> **username_0**: This is certainly based on that but my question is more specifically about how to remove the duplicate extension in the name.  

---

## 1 answers

### Answer by username_1 on 2017-12-01. Score: 1

Just tested your code and it worked as you'd expect if you leave off the extra getExtension() call (the filename property already includes the extension).
$filename = strtolower($asset->filename);

If it's not saving for you with that, you've got some other error happening elsewhere.


---

