Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Data Model Questions

Rename image on upload


Reply

3 Posts   1011 Views

Avatar
pingu

26 April 2011 at 11:21pm (Last edited: 26 April 2011 11:22pm), Community Member, 75 Posts

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.

Avatar
pingu

27 April 2011 at 3:22am (Last edited: 27 April 2011 3:30am), Community Member, 75 Posts

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.

Avatar
Craftnet

5 October 2012 at 12:25pm Community Member, 58 Posts

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

Sorry for my bad English