2

We have a front-end form that allows users to update their profile photo. If a user attempts to upload a non-image file, instead of the actionSaveUser controller failing and throwing an error, it gets to $this->_processUserPhoto and then throws an Exception

             {
200                 // Invalid SVG. Maybe it's missing its DTD?
201                 $svg = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>'.$svg;
202                 $this->_image = $this->_instance->load($svg);
203             }
204         }
205         else
206         {
207             $imageInfo = @getimagesize($path);
208 
209             if (!is_array($imageInfo))
210             {
211                 throw new Exception(Craft::t('The file “{path}” does not appear to be an image.', array('path' => $path)));
212             }

Is there a more graceful way to handle this so I can return an error to the user instead?

1

Update:

As of the next (post 2.4.2702) Craft release, Craft will now return a validation error on the UserModel if a non-image is uploaded for the user's profile photo.


I consider this a bug in Craft and we've already got a ticket open to address it.

Until then, I can think of two options work work around it, either of them particularly pretty. 1. Hack core Craft files to do the detection. 2. Have some JavaScript on your upload form that detects the extension of the file they're trying to upload.

  • 1
    Would it be an option to use a custom plugin controller to do server side validation on just the file upload and then, if it's valid, trigger the default controller using craft()->runController('users/saveUser'); but otherwise return an error? – John O Nov 5 '15 at 0:41
  • Actually, that's a way better idea. :) – Brad Bell Nov 5 '15 at 0:41
  • Cool! Does Craft have any built in functions I can use to determine MIME type, or check to make sure a file is an image? – John O Nov 5 '15 at 1:24
  • 1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.