php - sfValidatorFile 'mime_type' validation not working in production server


I've built a backend with a couple of models, one of them, Products, supports image uploading. Locally the file uploading works very fine. When I did the deploy, it stopped working: the form validation fails because of wrong mime type (error is: "Invalid mime type (jpeg image data, exif standard).") for the image field, even when i'm using the same JPG image that i used to test locally.

I'll show you my form class code:

class ProductForm extends BaseProductForm
  public function configure()

    /* Widgets */
    $this->widgetSchema['image_1'] = new sfWidgetFormInputFileEditable(array(
      'file_src' => '/'.basename(sfConfig::get('sf_upload_dir')).'/products/normal_'.$this->getObject()->getImage_1(),
      'is_image' => true,
      'edit_mode' => strlen($this->getObject()->getImage_1()) > 0,
      'delete_label' => 'remover el archivo actual',
      'template'  => '<div>%file%<br />%input%</div>'

    /* Validators */
    $this->validatorSchema['image_1'] = new sfValidatorFileImage(array(
      'required' => (! $this->getObject()->getImage_1()),
      'max_size' => '5252880',
      'path' => sfConfig::get('sf_upload_dir').'/products/original',
      'mime_types' => 'web_images',
      'min_width' =>  470,
      'max_width' => 99999,
      'min_height' =>  306,
      'max_height' => 99999,
      'validated_file_class' => 'sfResizedFile',   // This class creates the thumbnails
    ), array(
      'required' => 'Ten&eacute;s que seleccionar una imagen principal.',
      'max_size' => 'El tama&ntilde;o m&aacute;ximo es 5 MB',
      //'mime_types' => 'S&oacute;lo se permiten im&aacute;genes para web (jpg, png, gif)',
      'invalid_image' => '%value% no es un archivo de imagen.',
      'min_width' => 'El ancho de "%value%" es muy chico (mínimo %min_width% pixels).',
      'min_height' => 'El alto de "%value%" es muy chico (mínimo %min_height% pixels).',
    $this->validatorSchema['image_1_delete'] = new sfValidatorPass();

sfValidatorFileImage is just a custom validator class that extends sfValidatorFile with size checks. I've tried with the regular sfValidatorFile class just in case, but the error persists. Does mime types have something to do with server configuration? And why would it break on a regular web server?

Speed in answer is really appreciated since this was needed deployed today.




The only solution I could find is commenting the first item in the array of "mime_type_guessers" option of the sfValidatorFile class, the one that reads "guessFromFileinfo", specifically. Here is the code:

(lib/vendor/symfony/lib/validator/sfValidatorFile.class.php - line 62 aprox.)

$this->addOption('mime_type_guessers', array(
  //array($this, 'guessFromFileinfo'),
  array($this, 'guessFromMimeContentType'),
  array($this, 'guessFromFileBinary'),

Also, copying this file to lib/ and commenting the line in that file didn't worked, Symfony did always used the one in lib/vendor/symfony/lib/ so I did the change there.




If your OS is CentOS mime_content_type function returns mime type and additional charset for example instead of 'image/jpeg' you will receive 'image/jpeg; charset=binary'

there is a solution here:

edit sfValidatorFile.class.php and add this code after this line:

$mimeType = $this->getMimeType((string) $value['tmp_name'], (string) $value['type']);

here is code:

$mimeType = substr($mimeType, 0, strpos($mimeType, ';'));




Also try to do not use mimes collections (web_images). use array instead:

$mime_types = array (

$this->setValidator('filename', new sfValidatorFile(array(
  'mime_types' => $mime_types,

People are also looking for solutions to the problem: Converting a string into a key->value array in PHP


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.