Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Upload an image
- *
- * @param $inputFileName string
- * @param $path string
- * @param $data array
- * @return array
- */
- public function uploadImage($inputFileName, $path, &$data, $maxWidth, $maxHeight, $maxFileSize = 50000)
- {
- $helper = Mage::helper('toolbox');
- $io = new Varien_Io_File();
- $io->mkdir($path);
- if (isset($_FILES[$inputFileName]['name']) && (file_exists($_FILES[$inputFileName]['tmp_name']))) {
- try {
- try {
- if (isset($data[$inputFileName]['value'])) {
- $file = $path . '/' . $data[$inputFileName]['value'];
- if (file_exists($file)) {
- unlink($file);
- }
- }
- // Allowed extensions & mime types
- $allowedExtensions = array('jpg', 'jpeg', 'gif', 'png');
- $allowedMimeTypes = array('image/jpeg', 'image/gif', 'image/png');
- // Upload
- $uploader = new Varien_File_Uploader($inputFileName);
- $uploader->setAllowedExtensions($allowedExtensions);
- $uploader->setAllowRenameFiles(true);
- // Check mime type
- if ($uploader->checkMimeType($allowedMimeTypes)) {
- $upload = new Zend_File_Transfer_Adapter_Http();
- // Maximum filesize
- if ($maxFileSize != null) {
- $upload->addValidator('FilesSize', false, array('max' => $maxFileSize));
- }
- // Maximum width
- if ($maxWidth != null) {
- $upload->addValidator('ImageSize', false, array('maxwidth' => $maxWidth));
- }
- // Maximum height
- if ($maxHeight != null) {
- $upload->addValidator('ImageSize', false, array('maxheight' => $maxHeight));
- }
- // Valid?
- if ($upload->isValid($inputFileName)) {
- $name = $_FILES[$inputFileName]['name'];
- $uploader->save($path, $name);
- $data[$inputFileName] = $name;
- return true;
- }
- else {
- return $upload->getMessages();
- }
- }
- else {
- return $helper->__('Image type is not allowed (allowed types : jpg, gif, png)');
- }
- }
- catch (Exception $e) {
- Mage::log('Error : ' . $e->getMessage() . ' - Line ' . $e->getLine() . ' in ' . $e->getFile(), NULL, 'upload_images.log');
- return $helper->__('Error during image upload');
- }
- }
- catch (Exception $e) {
- Mage::log('Error : ' . $e->getMessage() . ' - Line ' . $e->getLine() . ' in ' . $e->getFile(), NULL, 'upload_images.log');
- return $helper->__('Error during image upload');
- }
- }
- }
Add Comment
Please, Sign In to add comment