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

Image subfolder


Go to End


10 Posts   5128 Views

Avatar
Terminator4

Community Member, 81 Posts

29 March 2009 at 11:48pm

Hey guys,
I would like to know how I can make it that when I upload images through my custom Page (ie Categories) in the CMS that I can then have the images saved in assets/categories/imageX.jpg

Thanks

Avatar
theAlien

Community Member, 131 Posts

30 March 2009 at 7:05am

Do you mean you want to save them in a specific folder other than the default /Uploads/ folder (/categories/) or in a specific name (/imageX.jpg)?

Avatar
Terminator4

Community Member, 81 Posts

30 March 2009 at 7:39am

Yes, in a specific folder but a good simple solution. I am not worried about the name since that is pretty straight forward.

Avatar
theAlien

Community Member, 131 Posts

30 March 2009 at 8:43am

OK, this might seem a not so simple solution, but in the end: it is pretty easy.

First a small explanation:
Most likely you're using a field from the type ImageField. This can be found in sapphire/forms/ImageField.php
If you open that, you'll see it's a subclass of filefield (ImageField extends FileField). This means: it's using all the features of sapphire/forms/FileField.php unless those features are overridden in ImageField.php

Well... what the SilverStripe devs are doing, can easily be done by yourself (avoid messing around in the core) :D

1) Make a new folder in the mysite-folder, for example: mysite/formfields/
2) Create in that folder a file like myImageField.php
3) Write in the first two lines:

<?php
class myImageField extends ImageField{

4) Now look in ImageField.php and FileField.php if you can find a place where the defaultfolder is set. It happens to be in FileField.php with the following line of code (line 64):
protected $folderName = 'Uploads';

5) Copy that line to your myImageField.php -file and paste it in there. Replace 'Uploads' with 'Categories'.
6) Close everything with these two lines:
}
?>

7) Instead of calling new ImageField in mysite/code/Categories.php, you should now call new myImageField.
8) Don't forget to run a /db/build

That should be all.

BTW: I have never tried replacing the default-folder. However I changed the allowedMaxFileSize and that worked fine.
If it doesn't work, please post it here.

Avatar
Terminator4

Community Member, 81 Posts

30 March 2009 at 9:30am

Umm,
Thanks. That seems like a very simple solution.

I guess if I want to make the folder dynamic such as make the folder be the name of the page's url segment then I should be able to replace that with the code to allow for that. I just hope the constructor works correctly in order to allow for that to work otherwise I will need to pass a variable into some method.

What do you think?

Avatar
theAlien

Community Member, 131 Posts

30 March 2009 at 11:27am

Edited: 30/03/2009 11:29am

It's worth a try. And if it doesn't work, some kind of method could work.

I'm not sure though if I would use the URLSegment.
The nice part of the ImageField is that you can reuse images. And my suspicions are that they won't move into another folder if you reused an image from let's say the About-us page on the Contact-us page. So in that case the logic of the image-part of your assets-folder would get a terrible mess (let alone the number of folders you could get)

Ordering them dynamic by ClassName sounds better to me. But I would prefer it more or less hardcoded by function (banner, sidebar etc) or just a plain Images-folder.

By the way: whatever you are planning, I would test it with a hardcoded Images-folder before I would start any coding. Remember: I didn't test it myself.

Avatar
vancouverWill

Community Member, 121 Posts

8 September 2009 at 11:42am

<input class="hidden" type="hidden" id="Form_EditImageForm_folderName" name="folderName" value="Uploads/projects" />

I tried this out so many different ways and I can see from the souce code that the foldername is being changed but the form still always saves it to uploads, so frustrating. Not sure what I am missing out on here but any advice would be really appreciated

Cheers

Will

Avatar
Willr

Forum Moderator, 5523 Posts

8 September 2009 at 5:49pm

If this is on your custom page then you have defined a custom getCMSFields() to add the image field. You can pass the folder name to this image field and it should use that dir rather then the built in one.

new ImageField($name, $title, '', '', '', $folderName = null);

Go to Top