/** * After save * * @param Varien_Object $object * @return Mage_Catalog_Model_Resource_Product_Attribute_Backend_Image */ public function afterSave($object) { $value = $object->getData($this->getAttribute()->getName()); if (is_array($value) && !empty($value['delete'])) { $object->setData($this->getAttribute()->getName(), ''); $this->getAttribute()->getEntity()->saveAttribute($object, $this->getAttribute()->getName()); return; } try { $uploader = new Mage_Core_Model_File_Uploader($this->getAttribute()->getName()); $uploader->setAllowedExtensions(array('jpg', 'jpeg', 'gif', 'png')); $uploader->setAllowRenameFiles(true); $uploader->setFilesDispersion(true); $uploader->addValidateCallback(Mage_Core_Model_File_Validator_Image::NAME, new Mage_Core_Model_File_Validator_Image(), "validate"); $uploader->save(Mage::getBaseDir('media') . '/catalog/product'); $fileName = $uploader->getUploadedFileName(); if ($fileName) { $object->setData($this->getAttribute()->getName(), $fileName); $this->getAttribute()->getEntity()->saveAttribute($object, $this->getAttribute()->getName()); } } catch (Exception $e) { return $this; } return $this; }
public function uploadAction() { try { $pattern = "/([0-9]+\\.[0-9]+\\.[0-9]+)(?:\\.[0-9]+)*/"; $matches = array(); preg_match($pattern, Mage::getVersion(), $matches); if (version_compare($matches[1], '1.5.1', '<')) { $uploader = new Varien_File_Uploader('image'); } else { $uploader = new Mage_Core_Model_File_Uploader('image'); } $uploader->setAllowedExtensions(array('jpg', 'jpeg', 'gif', 'png')); $uploader->addValidateCallback('catalog_product_image', Mage::helper('catalog/image'), 'validateUploadFile'); $uploader->setAllowRenameFiles(true); $uploader->setFilesDispersion(true); $result = $uploader->save($this->getMagicslideshowBaseMediaPath()); /** * Workaround for prototype 1.7 methods "isJSON", "evalJSON" on Windows OS */ $result['tmp_name'] = str_replace(DS, "/", $result['tmp_name']); $result['path'] = str_replace(DS, "/", $result['path']); $result['url'] = $this->getMagicslideshowMediaUrl($result['file']); $result['file'] = $result['file']; $result['cookie'] = array('name' => session_name(), 'value' => $this->_getSession()->getSessionId(), 'lifetime' => $this->_getSession()->getCookieLifetime(), 'path' => $this->_getSession()->getCookiePath(), 'domain' => $this->_getSession()->getCookieDomain()); } catch (Exception $e) { $result = array('error' => $e->getMessage(), 'errorcode' => $e->getCode()); } $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result)); }
public function beforeFieldSave($value, $oldValue) { $value = parent::beforeFieldSave($value, $oldValue); $template = $this->getTemplate(); /** @var $fileHelper Webguys_Easytemplate_Helper_File */ $fileHelper = Mage::helper('easytemplate/file'); $destinationPath = $fileHelper->getDestinationFilePath($template->getGroupId(), $template->getId()); if ($oldValue && ($value && $oldValue != $value || $this->_deleteFile)) { // Delete the old file $oldFilePath = sprintf('%s/%s', $destinationPath, $oldValue); if (file_exists($oldFilePath)) { @unlink($oldFilePath); } } if ($value) { $fileHelper->createTmpPath($template->getGroupId(), $template->getId()); if ($this->uploadComplete()) { $uploaderData = array('tmp_name' => $this->extractFilePostInformation('tmp_name'), 'name' => $value); $uploader = new Mage_Core_Model_File_Uploader($uploaderData); //$uploader->setAllowedExtensions(array('jpg','jpeg','gif','png','pdf')); $uploader->addValidateCallback('easytemplate_template_file', $fileHelper, 'validateUploadFile'); $uploader->setAllowRenameFiles(false); $uploader->setFilesDispersion(false); $result = $uploader->save($destinationPath); Mage::dispatchEvent('easytemplate_upload_file_after', array('result' => $result)); $value = $result['file']; } else { // TODO: Error handling } } return $value; }
/** * Save uploaded file and set its name to category * * @param Varien_Object $object */ public function afterSave($object) { $value = $object->getData($this->getAttribute()->getName()); if (is_array($value) && !empty($value['delete'])) { $object->setData($this->getAttribute()->getName(), ''); $this->getAttribute()->getEntity()->saveAttribute($object, $this->getAttribute()->getName()); return; } $path = Mage::getBaseDir('media') . DS . 'catalog' . DS . 'category' . DS; try { $uploader = new Mage_Core_Model_File_Uploader($this->getAttribute()->getName()); $uploader->setAllowedExtensions(array('jpg', 'jpeg', 'gif', 'png')); $uploader->setAllowRenameFiles(true); $uploader->addValidateCallback(Mage_Core_Model_File_Validator_Image::NAME, new Mage_Core_Model_File_Validator_Image(), "validate"); $result = $uploader->save($path); $object->setData($this->getAttribute()->getName(), $result['file']); $this->getAttribute()->getEntity()->saveAttribute($object, $this->getAttribute()->getName()); } catch (Exception $e) { if ($e->getCode() != Mage_Core_Model_File_Uploader::TMP_NAME_EMPTY) { Mage::logException($e); } /** @TODO ??? */ return; } }
/** * Save uploaded file before saving config value * * @return Mage_Backend_Model_Config_Backend_File */ protected function _beforeSave() { $value = $this->getValue(); $tmpName = $this->_requestData->getTmpName($this->getPath()); if ($tmpName) { $uploadDir = $this->_getUploadDir(); try { $file = array(); $file['tmp_name'] = $tmpName; $file['name'] = $this->_requestData->getName($this->getPath()); $uploader = new Mage_Core_Model_File_Uploader($file); $uploader->setAllowedExtensions($this->_getAllowedExtensions()); $uploader->setAllowRenameFiles(true); $uploader->addValidateCallback('size', $this, 'validateMaxSize'); $result = $uploader->save($uploadDir); } catch (Exception $e) { Mage::throwException($e->getMessage()); return $this; } $filename = $result['file']; if ($filename) { if ($this->_addWhetherScopeInfo()) { $filename = $this->_prependScopeInfo($filename); } $this->setValue($filename); } } else { if (is_array($value) && !empty($value['delete'])) { $this->setValue(''); } else { $this->unsValue(); } } return $this; }
/** * Save uploaded file before saving config value * * @return Mage_Adminhtml_Model_System_Config_Backend_File */ protected function _beforeSave() { /* print 'GroupID = '.$this->getGroupId().'<br>Field = '.$this->getField(); print "<pre>"; print_r($_FILES); print "</pre>"; die; */ $value = $this->getValue(); if ($_FILES['groups']['tmp_name'][$this->getGroupId()]['fields'][$this->getField()]['value']) { //$uploadDir = $this->_getUploadDir(); $uploadDir = Mage::getBaseDir('media') . DIRECTORY_SEPARATOR . 'productvideos' . DIRECTORY_SEPARATOR; try { $file = array(); $tmpName = $_FILES['groups']['tmp_name']; $file['tmp_name'] = $tmpName[$this->getGroupId()]['fields'][$this->getField()]['value']; $name = $_FILES['groups']['name']; $file['name'] = $name[$this->getGroupId()]['fields'][$this->getField()]['value']; /* extra code added to add time stamp to file name to get latest on top */ $file['name'] = date('YmdHis') . '_' . $file['name']; $uploader = new Mage_Core_Model_File_Uploader($file); $uploader->setAllowedExtensions($this->_getAllowedExtensions()); $uploader->setAllowRenameFiles(true); $uploader->addValidateCallback('size', $this, 'validateMaxSize'); $result = $uploader->save($uploadDir); } catch (Exception $e) { Mage::throwException($e->getMessage()); return $this; } $filename = $result['file']; if ($filename) { if ($this->_addWhetherScopeInfo()) { $filename = $this->_prependScopeInfo($filename); } $this->setValue($filename); } } else { if (is_array($value) && !empty($value['delete'])) { $this->setValue(''); } else { $this->unsValue(); } } return $this; }
/** * Return the root part of directory path for uploading * * @var string * @return string */ protected function _beforeSave() { $value = $this->getValue(); if ($_FILES['groups']['tmp_name'][$this->getGroupId()]['fields'][$this->getField()]['value']) { $uploadDir = $this->_getUploadDir(); try { $file = array(); $tmpName = $_FILES['groups']['tmp_name']; $file['tmp_name'] = $tmpName[$this->getGroupId()]['fields'][$this->getField()]['value']; $name = $_FILES['groups']['name']; $file['name'] = $name[$this->getGroupId()]['fields'][$this->getField()]['value']; //Check exists if (file_exists($uploadDir . DS . $file['name'])) { $msg = "+ " . Mage::helper('jmbasetheme')->__("Image with named %s was existed", $file['name']); Mage::getSingleton('core/session')->addNotice($msg); return $this; } else { $uploader = new Mage_Core_Model_File_Uploader($file); $uploader->setAllowedExtensions($this->_getAllowedExtensions()); $uploader->setAllowRenameFiles(true); $maxImageSize = 2 * 1024; //2MB $this->setMaxUploadFileSize($maxImageSize); $uploader->addValidateCallback('size', $this, 'validateMaxSize'); $result = $uploader->save($uploadDir); } } catch (Exception $e) { Mage::throwException($e->getMessage()); return $this; } $filename = $result['file']; if ($filename) { if ($this->_addWhetherScopeInfo()) { $filename = $this->_prependScopeInfo($filename); } $this->setValue($filename); } } else { if (is_array($value) && !empty($value['delete'])) { $this->setValue(''); } else { $this->unsValue(); } } return $this; }
public function uploadAction() { try { $uploader = new Mage_Core_Model_File_Uploader('image'); $uploader->setAllowedExtensions(array('jpg', 'jpeg', 'gif', 'png')); $uploader->addValidateCallback('catalog_product_image', Mage::helper('catalog/image'), 'validateUploadFile'); $uploader->setAllowRenameFiles(true); $uploader->setFilesDispersion(true); $result = $uploader->save(Mage::getSingleton('catalog/product_media_config')->getBaseTmpMediaPath()); $result['url'] = Mage::getSingleton('catalog/product_media_config')->getTmpMediaUrl($result['file']); $result['file'] = $result['file'] . '.tmp'; $result['cookie'] = array('name' => session_name(), 'value' => $this->_getSession()->getSessionId(), 'lifetime' => $this->_getSession()->getCookieLifetime(), 'path' => $this->_getSession()->getCookiePath(), 'domain' => $this->_getSession()->getCookieDomain()); } catch (Exception $e) { $result = array('error' => $e->getMessage(), 'errorcode' => $e->getCode()); } $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result)); }
/** * Return the root part of directory path for uploading * * @var string * @return string */ protected function _beforeSave() { $value = $this->getValue(); if ($_FILES['groups']['tmp_name'][$this->getGroupId()]['fields'][$this->getField()]['value']) { $uploadDir = $this->_getUploadDir(); try { $file = array(); $tmpName = $_FILES['groups']['tmp_name']; $file['tmp_name'] = $tmpName[$this->getGroupId()]['fields'][$this->getField()]['value']; $name = $_FILES['groups']['name']; $file['name'] = $name[$this->getGroupId()]['fields'][$this->getField()]['value']; $url = getimagesize($uploadDir . DS . $file['name']); //print_r($url); returns an array if (is_array($url)) { $fieldConfig = $this->getFieldConfig(); Mage::throwException($fieldConfig->label . Mage::helper('core')->__("with same name existed")); } else { $uploader = new Mage_Core_Model_File_Uploader($file); $uploader->setAllowedExtensions($this->_getAllowedExtensions()); $uploader->setAllowRenameFiles(true); $uploader->addValidateCallback('size', $this, 'validateMaxSize'); $result = $uploader->save($uploadDir); } } catch (Exception $e) { Mage::throwException($e->getMessage()); return $this; } $filename = $result['file']; if ($filename) { if ($this->_addWhetherScopeInfo()) { $filename = $this->_prependScopeInfo($filename); } $this->setValue($filename); } } else { if (is_array($value) && !empty($value['delete'])) { $this->setValue(''); } else { $this->unsValue(); } } return $this; }
/** * Save uploaded file before saving config value * * @return Mage_Adminhtml_Model_System_Config_Backend_File */ protected function _beforeSave() { $value = $this->getValue(); if ($_FILES['groups']['tmp_name'][$this->getGroupId()]['fields'][$this->getField()]['value']) { //$uploadDir = $this->_getUploadDir(); $uploadDir = Mage::getStoreConfig('zipcodeimport/paths/zipcodeIn'); try { $file = array(); $tmpName = $_FILES['groups']['tmp_name']; $file['tmp_name'] = $tmpName[$this->getGroupId()]['fields'][$this->getField()]['value']; $name = $_FILES['groups']['name']; $file['name'] = $name[$this->getGroupId()]['fields'][$this->getField()]['value']; /* extra code added to add time stamp to file name to get latest on top */ $file['name'] = date('YmdHis') . '_' . $file['name']; /*Extra code ends */ $uploader = new Mage_Core_Model_File_Uploader($file); $uploader->setAllowedExtensions($this->_getAllowedExtensions()); $uploader->setAllowRenameFiles(true); $uploader->addValidateCallback('size', $this, 'validateMaxSize'); $result = $uploader->save($uploadDir); } catch (Exception $e) { Mage::throwException($e->getMessage()); return $this; } $filename = $result['file']; if ($filename) { if ($this->_addWhetherScopeInfo()) { $filename = $this->_prependScopeInfo($filename); } $this->setValue($filename); } } else { if (is_array($value) && !empty($value['delete'])) { $this->setValue(''); } else { $this->unsValue(); } } return $this; }
public function uploadAction() { try { $uploader = new Mage_Core_Model_File_Uploader('image'); $uploader->setAllowedExtensions(array('jpg', 'jpeg', 'gif', 'png')); $uploader->addValidateCallback('catalog_product_image', Mage::helper('Mage_Catalog_Helper_Image'), 'validateUploadFile'); $uploader->setAllowRenameFiles(true); $uploader->setFilesDispersion(true); $result = $uploader->save(Mage::getSingleton('Mage_Catalog_Model_Product_Media_Config')->getBaseTmpMediaPath()); /** * Workaround for prototype 1.7 methods "isJSON", "evalJSON" on Windows OS */ $result['tmp_name'] = str_replace(DS, "/", $result['tmp_name']); $result['path'] = str_replace(DS, "/", $result['path']); $result['url'] = Mage::getSingleton('Mage_Catalog_Model_Product_Media_Config')->getTmpMediaUrl($result['file']); $result['file'] = $result['file'] . '.tmp'; $result['cookie'] = array('name' => session_name(), 'value' => $this->_getSession()->getSessionId(), 'lifetime' => $this->_getSession()->getCookieLifetime(), 'path' => $this->_getSession()->getCookiePath(), 'domain' => $this->_getSession()->getCookieDomain()); } catch (Exception $e) { $result = array('error' => $e->getMessage(), 'errorcode' => $e->getCode()); } $this->getResponse()->setBody(Mage::helper('Mage_Core_Helper_Data')->jsonEncode($result)); }
/** * Save uploaded file before saving config value * * @return Mage_Adminhtml_Model_System_Config_Backend_File */ protected function _beforeSave() { $value = $this->getValue(); if ($_FILES['groups']['tmp_name'][$this->getGroupId()]['fields'][$this->getField()]['value']) { $uploadDir = $this->_getUploadDir(); try { $file = array(); $tmpName = $_FILES['groups']['tmp_name']; $file['tmp_name'] = $tmpName[$this->getGroupId()]['fields'][$this->getField()]['value']; $name = $_FILES['groups']['name']; $file['name'] = $name[$this->getGroupId()]['fields'][$this->getField()]['value']; $uploader = new Mage_Core_Model_File_Uploader($file); $uploader->setAllowedExtensions($this->_getAllowedExtensions()); $uploader->setAllowRenameFiles(true); $uploader->addValidateCallback('size', $this, 'validateMaxSize'); $result = $uploader->save($uploadDir); } catch (Exception $e) { Mage::throwException($e->getMessage()); return $this; } $filename = $result['file']; if ($filename) { if ($this->_addWhetherScopeInfo()) { $filename = $this->_prependScopeInfo($filename); } $this->setValue($filename); } } else { if (is_array($value) && !empty($value['delete'])) { // Delete record before it is saved $this->delete(); // Prevent record from being saved, since it was just deleted $this->_dataSaveAllowed = false; } else { $this->unsValue(); } } return $this; }
/** * Add validators for uploading * * @param Mage_Core_Model_File_Uploader $uploader */ protected function addValidators(Mage_Core_Model_File_Uploader $uploader) { $uploader->addValidateCallback('size', $this, 'validateMaxSize'); }
/** * Save uploaded file before saving config value * * @return Mage_Adminhtml_Model_System_Config_Backend_File */ protected function _beforeSave() { $value = $this->getValue(); $uploadDir = Mage::getBaseDir('var') . '/import/relatedproducts/'; if ($_FILES['groups']['tmp_name'][$this->getGroupId()]['fields'][$this->getField()]['value']) { /* check directory exists if not create it */ if (is_dir($uploadDir)) { $directory_exists = true; } else { $directory_exists = false; mkdir($uploadDir, 777); } try { $filename = ''; $file = array(); $tmpName = $_FILES['groups']['tmp_name']; $file['tmp_name'] = $tmpName[$this->getGroupId()]['fields'][$this->getField()]['value']; $name = $_FILES['groups']['name']; $file['name'] = $name[$this->getGroupId()]['fields'][$this->getField()]['value']; $name_of_file = $file['name']; $filename = explode(".", $name_of_file); if (is_array($filename)) { if (sizeof($filename) == 2) { if ($filename[0] != 'import_products_relations') { $file['name'] = 'import_products_relations' . '.' . $filename[1]; } } else { Mage::throwException("Please remove any '.' from filename other than extension OR provide a missing extension to file."); } } else { Mage::throwException("Please provide a missing extension to file."); } /* extra code added to remove file from directory if it exists and upload new one */ if ($directory_exists) { $file_path = $uploadDir . '/' . trim($file['name']); if (is_file($file_path)) { unlink($file_path); } } /*Extra code ends */ $uploader = new Mage_Core_Model_File_Uploader($file); $uploader->setAllowedExtensions($this->_getAllowedExtensions()); $uploader->setAllowRenameFiles(true); $uploader->addValidateCallback('size', $this, 'validateMaxSize'); $result = $uploader->save($uploadDir); } catch (Exception $e) { Mage::throwException($e->getMessage()); return $this; } $filename = $result['file']; if ($filename) { if ($this->_addWhetherScopeInfo()) { $filename = $this->_prependScopeInfo($filename); } $this->setValue($filename); } } else { if (is_array($value) && !empty($value['delete'])) { $file_path = $uploadDir . '/' . trim($value['value']); if (is_file($file_path)) { unlink($file_path); } $this->setValue(''); } else { $this->unsValue(); } } return $this; }
public function uploadAction() { $helper = Mage::helper('productivity'); if (!$helper->isReviewerLogged()) { return $this->_error(); } if (!isset($_FILES['qqfile'])) { return $this->_error(); } if (!($product = $this->_getProduct($this->getRequest()->getParam('product_id')))) { return $this->_error(); } $uploader = new Mage_Core_Model_File_Uploader('qqfile'); $uploader->setAllowedExtensions(array('jpg', 'jpeg', 'gif', 'png')); $uploader->addValidateCallback('catalog_product_image', Mage::helper('catalog/image'), 'validateUploadFile'); $uploader->setAllowRenameFiles(true); $uploader->setFilesDispersion(true); $result = $uploader->save(Mage::getSingleton('catalog/product_media_config')->getBaseTmpMediaPath()); Mage::dispatchEvent('catalog_product_gallery_upload_image_after', array('result' => $result, 'action' => $this)); try { $file = $helper->add($product, $result); } catch (Exception $e) { Mage::logException($e); return $this->_error(); } $this->_success(array('file' => $file)); }
<?php /* Uploadify Copyright (c) 2012 Reactive Apps, Ronnie Garcia Released under the MIT License <http://www.opensource.org/licenses/mit-license.php> */ // Define a destination //$targetFolder = '/apr/trunk/magento/media/uploads'; // Relative to the root require_once '../../app/Mage.php'; Mage::app(); Mage::getSingleton('core/session'); try { $uploader = new Mage_Core_Model_File_Uploader('Filedata'); $uploader->setAllowedExtensions(array('jpg', 'jpeg', 'gif', 'png')); $uploader->addValidateCallback('catalog_product_image', Mage::helper('catalog/image'), 'validateUploadFile'); $uploader->setAllowRenameFiles(true); $uploader->setFilesDispersion(true); $result = $uploader->save(Mage::getSingleton('catalog/product_media_config')->getBaseTmpMediaPath()); // Mage::dispatchEvent('catalog_product_gallery_upload_image_after', array( // 'result' => $result, // 'action' => $this // )); /** * Workaround for prototype 1.7 methods "isJSON", "evalJSON" on Windows OS */ $result['tmp_name'] = str_replace(DS, "/", $result['tmp_name']); $result['path'] = str_replace(DS, "/", $result['path']); $result['url'] = Mage::getSingleton('catalog/product_media_config')->getTmpMediaUrl($result['file']); $result['file'] = $result['file'] . '.tmp'; } catch (Exception $e) {
/** * Save uploaded file and set its name to category * * @param Varien_Object $object */ public function afterSave($object) { if (!Mage::helper('uaudio_storage')->isEnabled()) { return parent::afterSave($object); } $value = $object->getData($this->getAttribute()->getName()); $path = Mage::getBaseDir('media') . DS . 'catalog' . DS . 'category' . DS; if (is_array($value) && !empty($value['delete'])) { $model = Mage::getModel('core/file_storage')->getStorageModel(); $model->deleteFile($path . $value['value']); $object->setData($this->getAttribute()->getName(), ''); $this->getAttribute()->getEntity()->saveAttribute($object, $this->getAttribute()->getName()); } try { $uploader = new Mage_Core_Model_File_Uploader($this->getAttribute()->getName()); $uploader->setAllowedExtensions(array('jpg', 'jpeg', 'gif', 'png')); $uploader->setAllowRenameFiles(true); if (class_exists('Mage_Core_Model_File_Validator_Image')) { // added for mage patch SUPEE-7405 $uploader->addValidateCallback(Mage_Core_Model_File_Validator_Image::NAME, new Mage_Core_Model_File_Validator_Image(), "validate"); } $result = $uploader->save($path); $object->setData($this->getAttribute()->getName(), $result['file']); $this->getAttribute()->getEntity()->saveAttribute($object, $this->getAttribute()->getName()); } catch (Exception $e) { if ($e->getCode() != Mage_Core_Model_File_Uploader::TMP_NAME_EMPTY) { Mage::logException($e); } return null; } }