Jump to:

23483 Posts in 18965 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » UploadField limiting allowed file extensions

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

Page: 1
Go to End
Author Topic: 1202 Views
  • kinglozzer
    Avatar
    Community Member
    139 Posts

    UploadField limiting allowed file extensions Link to this post

    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

  • ordinarywebguy
    Avatar
    Community Member
    13 Posts

    Re: UploadField limiting allowed file extensions Link to this post

    Try this:

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

  • kinglozzer
    Avatar
    Community Member
    139 Posts

    Re: UploadField limiting allowed file extensions Link to this post

    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

  • kinglozzer
    Avatar
    Community Member
    139 Posts

    Re: UploadField limiting allowed file extensions Link to this post

    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!

    1202 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.