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   1342 Views

Avatar
kinglozzer

Community Member, 171 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, 171 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, 171 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!