Hi, i have front end form, with file input field. When I upload it, I want to change file name to some random numbers or letters (ect. 4455asdasd54.jpg).
How can I do that? Maybe someone could provide simple code example?
This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.
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.
Hi, i have front end form, with file input field. When I upload it, I want to change file name to some random numbers or letters (ect. 4455asdasd54.jpg).
How can I do that? Maybe someone could provide simple code example?
Is this forum dead? :D
Is this forum dead?
no, maybe that isn't something someone can help you with :)
I'm sure you can get this by debugging and hacking the core code - not that I would advise that.
Hi, swaiba,
I'm glad that at least someone wrote back :)
Maybe where is method like setFolderName, just for files?
You could create a DataExtension for File and extend the method onAfterUpload().
Something like:
class MyFileExtension extends DataExtension {
function onAfterUpload() {
$file = $this->owner;
$file->Name = 'myFilename';
$file->write();
}
}
Not tested... but should work.
Saw this in the addons, thought it might be useful to you...
http://addons.silverstripe.org/add-ons/silverstripe/selectupload
Hi, tried what Devlin suggested, but it didn't worked out, so my solution was:
if(!empty($data['Photo1']['name'])){
$photo1 = File::get()->byID(Member::currentUser()->Photo1ID);
// getting ext
$ext1 = $photo1->getExtension();
$random1 = (int)substr(uniqid('', true), -7);
$randomDate1 = $random1 + (int)time();
$photo1->Name = 'sitename-' . $randomDate1 . $CurrentMemberID . $random1 . '.' . $ext1;
$photo1->write();
}
it's not perfect, but it worked.
Thank you all for the help :)