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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Custom upload directory


Go to End


11 Posts   5414 Views

Avatar
bummzack

Community Member, 904 Posts

15 November 2009 at 10:34pm

Edited: 15/11/2009 10:36pm

Hi Aaron

Of course you're right. I didn't read your code very carefully and was under the impression that you use Folder::findOrMake($this->URLSegment) each time "onBeforeWrite" is being called. Sorry, maybe it's because of the lacking indentation ;)

Changed my code in the previous example to use 'Folder' instead of 'File'.

The onBeforeWrite function on the DataObject would simply ensure that the file is located in the designated folder (defined by it's "holder" Page).

Avatar
Double-A-Ron

Community Member, 607 Posts

15 November 2009 at 10:47pm

Edited: 15/11/2009 10:47pm

Gotcha, cheers for that.

Yeah, it's strange. Copies from Dreamweaver to forum code blocks maintain indents. But from PHPEd to forum code blocks, the look fine, but they lose the tabs when I submit.

Cheers mate.
Aaron

Avatar
torleif2

Community Member, 6 Posts

15 September 2010 at 3:28pm

Custom upload folders are set by changing the ParentID of the file. The following code will save an image to the server, and set the folder name to 'Articles', then write it to the database:

 $ImageFile = new Image();
 $ImageFile->setName($_FILES['Image']['name']);
 $ImageFile->ParentID = Folder::findOrMake('Articles')->ID;
 $ImageFile->loadUploadedImage($_FILES['MainImageID']);
 $ImageFile->write();

(Note: there's a bug in SS 2.4.1 that makes loadUploadedImage() unusable. My image class is actually extends Image, with a overwritten loadUploadedImage function)

Go to Top