Guest User

Untitled

a guest
May 21st, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.31 KB | None | 0 0
  1. /**
  2.      * Upload an image
  3.      *
  4.      * @param $inputFileName string
  5.      * @param $path string
  6.      * @param $data array
  7.      * @return array
  8.      */
  9.     public function uploadImage($inputFileName, $path, &$data, $maxWidth, $maxHeight, $maxFileSize = 50000)
  10.     {
  11.         $helper = Mage::helper('toolbox');
  12.         $io = new Varien_Io_File();
  13.         $io->mkdir($path);
  14.  
  15.         if (isset($_FILES[$inputFileName]['name']) && (file_exists($_FILES[$inputFileName]['tmp_name']))) {
  16.             try {
  17.                 try {
  18.                     if (isset($data[$inputFileName]['value'])) {
  19.                         $file = $path . '/' . $data[$inputFileName]['value'];
  20.                         if (file_exists($file)) {
  21.                             unlink($file);
  22.                         }
  23.                     }
  24.  
  25.                     // Allowed extensions & mime types
  26.                     $allowedExtensions = array('jpg', 'jpeg', 'gif', 'png');
  27.                     $allowedMimeTypes = array('image/jpeg', 'image/gif', 'image/png');
  28.                     // Upload
  29.                     $uploader = new Varien_File_Uploader($inputFileName);
  30.                     $uploader->setAllowedExtensions($allowedExtensions);
  31.                     $uploader->setAllowRenameFiles(true);
  32.                     // Check mime type
  33.                     if ($uploader->checkMimeType($allowedMimeTypes)) {
  34.                         $upload = new Zend_File_Transfer_Adapter_Http();
  35.                         // Maximum filesize
  36.                         if ($maxFileSize != null) {
  37.                             $upload->addValidator('FilesSize', false, array('max' => $maxFileSize));
  38.                         }
  39.                         // Maximum width
  40.                         if ($maxWidth != null) {
  41.                             $upload->addValidator('ImageSize', false, array('maxwidth' => $maxWidth));
  42.                         }
  43.                         // Maximum height
  44.                         if ($maxHeight != null) {
  45.                             $upload->addValidator('ImageSize', false, array('maxheight' => $maxHeight));
  46.                         }
  47.                         // Valid?
  48.                         if ($upload->isValid($inputFileName)) {
  49.                             $name = $_FILES[$inputFileName]['name'];
  50.                             $uploader->save($path, $name);
  51.                             $data[$inputFileName] = $name;
  52.                             return true;
  53.                         }
  54.                         else {
  55.                             return $upload->getMessages();
  56.                         }
  57.                     }
  58.                     else {
  59.                         return $helper->__('Image type is not allowed (allowed types : jpg, gif, png)');
  60.                     }
  61.                 }
  62.                 catch (Exception $e) {
  63.                     Mage::log('Error : ' . $e->getMessage() . ' - Line ' . $e->getLine() . ' in ' . $e->getFile(), NULL, 'upload_images.log');
  64.                     return $helper->__('Error during image upload');
  65.                 }
  66.             }
  67.             catch (Exception $e) {
  68.                 Mage::log('Error : ' . $e->getMessage() . ' - Line ' . $e->getLine() . ' in ' . $e->getFile(), NULL, 'upload_images.log');
  69.                 return $helper->__('Error during image upload');
  70.             }
  71.         }
  72.     }
Add Comment
Please, Sign In to add comment