Jump to:

3373 Posts in 998 Topics by 712 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Rename image on upload

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

Page: 1
Go to End
Author Topic: 913 Views
  • pingu
    Avatar
    Community Member
    75 Posts

    Rename image on upload Link to this post

    Hi guys,

    What's the best way to rename an image on upload? I'd like to prefix image names with a unique id which matches the related dataobject's ID as they are uploaded.

    These images will be downloaded via FTP to perform bulk actions against after a period of time, and they need an identifier to link them to the dataobject with which they were uploaded.

  • pingu
    Avatar
    Community Member
    75 Posts

    Re: Rename image on upload Link to this post

    Okay, so I was able to do this by extending SimpleImageField (which extends FileField) and overriding FileField's saveInto() method like so:

    public function saveInto(DataObject $record) {
    ...
    $id = $_POST['id'];
    $filearr = $_FILES[$this->name];
    $ext = explode('.',$filearr['name']);
          
    $filearr['name'] = $id.'.'.strtolower(end($ext));
    $this->upload->loadIntoFile($filearr, $file, $this->folderName);

    ...
    }

    It works as I'd like for the front-end form, but isn't ideal as if images are uploaded via ModelAdmin, they're not renamed, so would appreciate suggestions for a more robust solution.

  • Craftnet
    Avatar
    Community Member
    58 Posts

    Re: Rename image on upload Link to this post

    Coudl you paste here all code SimpleImageField with your correction ( with saveInto() )?

    Sorry for my bad English

    913 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.