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.

General Questions

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

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

UploadField limiting allowed file extensions


Reply

4 Posts   1229 Views

Avatar
kinglozzer

4 July 2012 at 3:44am Community Member, 146 Posts

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

4 July 2012 at 5:50am Community Member, 13 Posts

Try this:

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

Avatar
kinglozzer

4 July 2012 at 8:01pm Community Member, 146 Posts

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

5 July 2012 at 11:27pm Community Member, 146 Posts

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!