Magento PDF Upload

After applying the Magento patch SUPEE-9767, it is no longer possible to upload PDFs to the Magento system via the WYSIWYG editor's upload functionality, even if this file type had previously been added to the list of allowed extensions via a suitable config.xml. This is due to the newly introduced validate() function, which throws an exception after an unsuccessful attempt to determine the file format using getimagesize(). We therefore need to prevent this exception from being triggered.

 

To do this, we create the file

 

\app\code\local\Mage\Core\Model\File\Validator\Image.php

 

as a copy of

 

\app\code\core\Mage\Core\Model\File\Validator\Image.php

 

and modify the validate() function. We extend the IF statement, which ends on line 118, with an ELSE case that handles the PDF format. We therefore change

 

imagedestroy($img);
imagedestroy($image);
return null;
} else {
throw Mage::exception('Mage_Core', Mage::helper('core')->__('Invalid image.'));
}
}
}

 

to

 

imagedestroy($img);
imagedestroy($image);
return null;
} else {
throw Mage::exception('Mage_Core', Mage::helper('core')->__('Invalid image.'));
}
}
} else {
if (mime_content_type($filePath)=='application/pdf') {
$fileType = IMAGETYPE_PNG;
return null;
}
throw Mage::exception('Mage_Core', Mage::helper('core')->__('Invalid MIME type.').' '.mime_content_type($filePath));
}

 

The additional exception we added is more of an optional extra. After this, the upload works again, although we are still informed that the image format is not supported—which does not matter, even though it is of course technically correct. Tested with Magento CE 1.9.2.4.