Answers.Body: <p><strong>Update:</strong></p>
  
  <p>As of the next (<a href="http://buildwithcraft.com/updates#build2702" rel="nofollow">post 2.4.2702</a>) Craft release, Craft will now return a validation error on the UserModel if a non-image is uploaded for the user's profile photo.</p>
  
  <hr>
  
  <p>I consider this a bug in Craft and we've already got a ticket open to address it.</p>
  
  <p>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.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-11-05T00:41:03.133Z
Answers.Text: 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?
Answers.UserDisplayName: John O
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-11-05T00:41:44.813Z
Answers.Text: Actually, that's a way better idea. :)
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-11-05T01:24:02.537Z
Answers.Text: 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?
Answers.UserDisplayName: John O
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-11-05T01:26:48.843Z
Answers.Text: http://buildwithcraft.com/classreference/helpers/IOHelper#getMimeType-detail and http://buildwithcraft.com/classreference/helpers/IOHelper#getMimeTypeByExtension-detail
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-11-04T23:44:14.717Z
Answers.LastActivityDate: 2015-11-09T23:33:11.280Z
Answers.LastEditDate: 2015-11-09T23:33:11.280Z
Answers.UserDisplayName: Brad Bell
Body: <p>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</p>
  
  <pre><code>             {
  200                 // Invalid SVG. Maybe it's missing its DTD?
  201                 $svg = '&lt;?xml version="1.0" encoding="UTF-8" standalone="no"?&gt;'.$svg;
  202                 $this-&gt;_image = $this-&gt;_instance-&gt;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' =&gt; $path)));
  212             }
  </code></pre>
  
  <p>Is there a more graceful way to handle this so I can return an error to the user instead?</p>
  
Tags: <form><user-profile><validation>
Title: Handling non-image uploads when updating user photo from front-end form
ThreadText: # Handling non-image uploads when updating user photo from front-end form
  
  (Asked by: username_0 on 2015-11-04)
  
  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?
  
  **Tags:** form, user-profile, validation
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2015-11-04. Score: 1
  
  **Update:**
  
  As of the next ([post 2.4.2702](http://buildwithcraft.com/updates#build2702)) 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.
  
  > **Comments:**
  > **username_0**: 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?  
  > **username_1**: Actually, that's a way better idea. :)  
  > **username_0**: 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?  
  > **username_1**: http://buildwithcraft.com/classreference/helpers/IOHelper#getMimeType-detail and http://buildwithcraft.com/classreference/helpers/IOHelper#getMimeTypeByExtension-detail  
  
  ---
  
  