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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

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

Extend Upload


Go to End


956 Views

Avatar
Optic Blaze

Community Member, 190 Posts

4 March 2014 at 4:03am

Hi there,

I am building some front end forms and the users need to upload files. What i need to to add a timestamp to each file name. So a file called apple.jpg should become 20140301-apple.jpg. As far as i can see i need to extend the Upload Object to do that.

I found on line 137 of the Upload Class, that if i change the way $file works (which is in the upload function) i can add my timestamp in by doing the following:
$filenamegenerator = date('Ymdhis')."-";
$file = $nameFilter->filter($filenamegenerator.$tmpFile['name']);

So now i want to extend the upload function.

So far i have the following:

_config.php
-------------------------------------------------
// Extend the controller
Controller::add_extension('Upload', 'MyUploadExtention');

MyUploadExtention .php
-------------------------------------------------
class MyUploadExtention extends Extension {

public function myload() {
function load() {
$filenamegenerator = date('Ymdhis')."-".rand(1,10000)."-";
$file = $nameFilter->filter($filenamegenerator.$tmpFile['name']);
}
$this->extend('alterload', this->load());
return $load;
}

}