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   1064 Views

Avatar
pingu

Community Member, 75 Posts

26 April 2011 at 11:21pm

Edited: 26/04/2011 11:22pm

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

Community Member, 75 Posts

27 April 2011 at 3:22am

Edited: 27/04/2011 3:30am

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

Community Member, 58 Posts

5 October 2012 at 12:25pm

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

Sorry for my bad English