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

UploadField limiting allowed file extensions


Go to End


6 Posts   7461 Views

Avatar
kinglozzer

Community Member, 187 Posts

4 July 2012 at 3:44am

Hi all,

I'm still relatively new to SilverStripe and have been trying to get to grips with things since 3.0.0 came out. I'm wondering if there's any way of only allowing certain file types to be uploaded with the UploadField? I can't seem to find anything in the API documentation for it.

I'm interested in limiting it to just images (only for an imaginary client's needs). The 'client' will need to be able to upload hundreds of images per DataObject, so I wasn't sure whether UploadField was the best thing to use.

Thanks

Avatar
ordinarywebguy

Community Member, 13 Posts

4 July 2012 at 5:50am

Try this:

$myField->allowedExtensions = array('jpg', 'gif', 'png');

Avatar
kinglozzer

Community Member, 187 Posts

4 July 2012 at 8:01pm

I did try that but I can still upload any type of file :/

This is my implementation of it:

$uploadField = new UploadField('Images');
$uploadField->allowedExtensions = array('jpg', 'gif', 'png');

$fields->addFieldToTab("Root.Images", $uploadField);

Thanks

Avatar
kinglozzer

Community Member, 187 Posts

5 July 2012 at 11:27pm

Okay, I've fixed this. In case anyone else gets stuck with it:

This

$uploadField->allowedExtensions = array('jpg', 'gif', 'png');

Should instead be this

$uploadField->getValidator()->allowedExtensions = array('jpg', 'gif', 'png');

I need some docs on this!

Avatar
merrick_sd

Community Member, 99 Posts

3 July 2015 at 1:25am

silverstripe 3.0.5

do i have to and anything anywhere else?

$uploadField = new UploadField('bannerimage','Banner Image');
$uploadField->setFolderName('Banners');
$uploadField->getValidator()->setAllowedExtensions(array('jpg','svg','png','jpeg','gif'));
$fields->addFieldToTab('Root.Main', $uploadField);

but i cannot upload a svg

i get
Extension is not allowed (valid: {extensions})

some other posts say i need to add a line in mysite/_config.php

eg
Image::$allowed_extensions[] = 'svg';

others say
Config::inst()->update('Image', 'allowed_extensions', array('svg', 'jpg','png'));

Attached Files
Avatar
Pyromanik

Community Member, 419 Posts

3 July 2015 at 10:51am

SVG is not an image, you should use File as the type for this.

Check that .svg is allowed in assets/.htaccess - if not, add it to the list.

Also consider upgrading from that old version, if you have time :)