Jump to:

23003 Posts in 11856 Topics by 2828 members

General Questions

SilverStripe Forums » General Questions » Renaming a file after it was uploaded

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 495 Views
  • smares
    Avatar
    Community Member
    25 Posts

    Renaming a file after it was uploaded Link to this post

    Hey guys, I am trying to figure out how to rename a file after it has been uploaded.

    I have a custom DataObject with a property called "ProductID" and a has_one relation to Image called "Icon". This DataObject is managed by a CTF and the getCMSFields returns (among other fields) an ImageField. What I want to do is when the user saves the DataObject and an icon is attached, it should have the product ID as name.

    After chatting on IRC, I tried to write the following in the onBeforeWrite method:

    parent::onBeforeWrite();

    $this->Icon()->Name = '123.' . $this->Icon()->Extension;
    $this->Icon()->write();

    The "123" is just a dummy. What happens is when the user selects an image from his computer and clicks on Attach image, two DB entries are created, for the file. One having "123." as filename and being in the root of the assets folder, and one having the original filename being in the directory specified in the ImageField. When the user clicks on "Save", the DB entry with the original filename is changed to reflect my desired "123.png" filename, however, the entry from the assets root "123." is still there. If I go to Files & Images and click on "Look for new files", that entry is removed.

    Any idea how I am supposed to rename files the right way?

    495 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.