# Rename files on upload to panel

**URL:** <https://forum.getkirby.com/t/rename-files-on-upload-to-panel/20299>\
**Category:** Questions\
**Tags:** v3\
**Created:** [December 8, 2020, 8:38pm UTC](https://forum.getkirby.com/t/rename-files-on-upload-to-panel/20299 "2020-12-08T20:38:59Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![elaltito](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/elaltito/32/7940_2.png) [@elaltito](https://forum.getkirby.com/u/elaltito)\
**Post date:** [December 8, 2020, 8:38pm UTC](https://forum.getkirby.com/t/rename-files-on-upload-to-panel/20299/1 "2020-12-08T20:38:59Z")

</div>

Hello!

I’m trying to find a way to automatically rename files on upload to the panel to :  
(Site name + Section name + Page name + Number).

I found these 2 topics that both already ask this question :

> [@Rename images on upload](https://forum.getkirby.com/t/rename-images-on-upload/3324):
>
> In the panel, it would be convenient for me to have the images automatically renaming themselves matching the page title on upload. For example: My page title is: product-title I upload 3 images by dragging them on the page view in the panel. They automatically get renamed to: product-title-01.jpg product-title-02.jpg product-title-03.jpg I then upload 2 images more. They get renamed to: product-title-04.jpg product-title-05.jpg Can I have some hints on how to achieve it?

> [@Rename file on upload hook](https://forum.getkirby.com/t/rename-file-on-upload-hook/18315):
>
> Hi! I’m trying to rename files on upload (Site name + page title + number) but unfortunately this doesn’t work confused Maybe someone has any good tips? 'hooks' =\> [ 'file.create:after' =\> function ($file) { $page = $file-\>page(); $title = $page-\>title(); $count = $page-\>files()-\>count() + 1; $filename = "Something-" . $title . "-" . $count; try { $file-\>rename($filename); } catch(Exception $e) { } } (…

I have tried both solutions, but neither of them is functioning for me… 😕

I tried creating a .php file at : **site/config/config.php** and copying the code for each of the above solutions into that file, but neither of them worked… any ideas ?

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [December 8, 2020, 8:47pm UTC](https://forum.getkirby.com/t/rename-files-on-upload-to-panel/20299/2 "2020-12-08T20:47:48Z")

</div>

The Kirby 2 code won’t work anymore.

Could you please post what you have so that we can take it from there? A file.create:after hook is definitely the way to go.

---

<div class="post-metadata">

**Author:** ![elaltito](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/elaltito/32/7940_2.png) [@elaltito](https://forum.getkirby.com/u/elaltito)\
**Post date:** [December 8, 2020, 10:13pm UTC](https://forum.getkirby.com/t/rename-files-on-upload-to-panel/20299/3 "2020-12-08T22:13:19Z")

</div>

This is the content of the config.php file :

```
<?php

return [
  'hooks' => [
'file.create:after' => function ($file) {
		      $page = $file->page();
		      $title = $page->title();
          $count = $page->files()->count() + 1;
          $filename = "Something-" . $title . "-" . $count;
		      try {
		          $file->changeName($filename);
		          } catch(Exception $e) {
		    }
	}
  ]
];
```

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [December 8, 2020, 10:27pm UTC](https://forum.getkirby.com/t/rename-files-on-upload-to-panel/20299/4 "2020-12-08T22:27:24Z")

</div>

Hm, just tested in a Starterkit and it works as expected.

---

<div class="post-metadata">

**Author:** ![elaltito](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/elaltito/32/7940_2.png) [@elaltito](https://forum.getkirby.com/u/elaltito)\
**Post date:** [December 8, 2020, 11:16pm UTC](https://forum.getkirby.com/t/rename-files-on-upload-to-panel/20299/5 "2020-12-08T23:16:16Z")

</div>

oh yes, good idea! 🙂

ok so I just tested it out on the Starter Kit too :  
– When I upload a single image it seems to work every time  
– When I upload multiple images (2 or more) it seem to work randomly…

I have attached a screenshot, these are 2 attempts at uploading the same 7 images.

 ![Screenshot 2020-12-09 at 00.05.00](https://europe1.discourse-cdn.com/flex017/uploads/getkirby/original/2X/6/6fa0ff83b013b71701230f8dc7af58a4b8b0c154.jpeg)

As you can see by the names highlighted in red; some are renamed, some aren’t.  
And it isn’t even the same images each time… 😕

Uploading to my site still doesn’t work at all… but I figure we should probably try to at least get it working properly in a clean starter kit before I try to make it work on my site 🙂

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [December 9, 2020, 8:04am UTC](https://forum.getkirby.com/t/rename-files-on-upload-to-panel/20299/6 "2020-12-09T08:04:24Z")

</div>

Same problem as here: [Hook to rename files only works with the first one](https://forum.getkirby.com/t/hook-to-rename-files-only-works-with-the-first-one/13849/5)

You could check if it makes a difference if you change your PHP settings, otherwise maybe create an issue on GitHub.

---

<div class="post-metadata">

**Author:** ![Guillaume](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/guillaume/32/9826_2.png) [@Guillaume](https://forum.getkirby.com/u/Guillaume)\
**Post date:** [May 5, 2021, 8:50am UTC](https://forum.getkirby.com/t/rename-files-on-upload-to-panel/20299/7 "2021-05-05T08:50:04Z")

</div>

Is it possible to fine tune the hook so it only rename images and not pdf or other type of files ?

Thank you !!

---

<div class="post-metadata">

**Author:** ![pixelijn](https://avatars.discourse-cdn.com/v4/letter/p/7ab992/32.png) [@pixelijn](https://forum.getkirby.com/u/pixelijn)\
**Post date:** [May 5, 2021, 9:11am UTC](https://forum.getkirby.com/t/rename-files-on-upload-to-panel/20299/8 "2021-05-05T09:11:23Z")

</div>

Yes, while the hook will be called on every file upload, inside the hook you can of course use any conditions you like.

---

<div class="post-metadata">

**Author:** ![Guillaume](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/guillaume/32/9826_2.png) [@Guillaume](https://forum.getkirby.com/u/Guillaume)\
**Post date:** [May 7, 2021, 9:27am UTC](https://forum.getkirby.com/t/rename-files-on-upload-to-panel/20299/9 "2021-05-07T09:27:25Z")

</div>

Thank you very much !
