public function importAction() { try { $productId = $this->getRequest()->getParam('id'); $fileName = $this->getRequest()->getParam('Filename'); $path = Mage::getBaseDir('var') . DS . 'import' . DS; $uploader = new Mage_Core_Model_File_Uploader('file'); $uploader->setAllowedExtensions(array('csv')); $uploader->setAllowRenameFiles(false); $uploader->setFilesDispersion(false); $result = $uploader->save($path, $fileName); $io = new Varien_Io_File(); $io->open(array('path' => $path)); $io->streamOpen($path . $fileName, 'r'); $io->streamLock(true); while ($data = $io->streamReadCsv(';', '"')) { if ($data[0]) { $model = Mage::getModel('giftcards/pregenerated')->load($data[0], 'card_code'); if ($model->getId()) { continue; } $model->setCardCode($data[0]); $model->setCardStatus(1); $model->setProductId($productId); $model->save(); } else { continue; } } } catch (Exception $e) { $result = array('error' => $e->getMessage(), 'errorcode' => $e->getCode()); } $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result)); }
/** * 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 saveFields(Varien_Event_Observer $observer) { $model = $observer->getEvent()->getPage(); $request = $observer->getEvent()->getRequest(); if (isset($_FILES['image']['name']) && $_FILES['image']['name'] != '') { try { $uploader = new Mage_Core_Model_File_Uploader('image'); $uploader->setAllowedExtensions(array('jpg', 'jpeg', 'gif', 'png')); $uploader->setAllowRenameFiles(true); $uploader->setFilesDispersion(false); $dirPath = Mage::getBaseDir('media') . DS . 'page' . DS; $result = $uploader->save($dirPath, $_FILES['image']['name']); } catch (Exception $e) { Mage::log($e->getMessage()); } $model->setImage('page/' . $result['file']); } else { $data = $request->getPost(); if (isset($data['image']) && isset($data['image']['delete']) && $data['image']['delete'] == 1) { $model->setImage(false); } elseif (isset($data['image']) && is_array($data['image'])) { $model->setImage($data['image']['value']); } } if (empty($model->getPageType())) { $model->setPageType(null); } }
/** * After attribute is saved upload file to media * folder and save it to its associated product. * * @param Mage_Catalog_Model_Product $object * @return Jvs_FileAttribute_Model_Attribute_Backend_File */ 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 { $uploadedFile = new Varien_Object(); $uploadedFile->setData('name', $this->getAttribute()->getName()); $uploadedFile->setData('allowed_extensions', array('jpg', 'jpeg', 'gif', 'png', 'tif', 'tiff', 'mpg', 'mpeg', 'mp3', 'wav', 'pdf', 'txt')); Mage::dispatchEvent('jvs_fileattribute_allowed_extensions', array('file' => $uploadedFile)); $uploader = new Mage_Core_Model_File_Uploader($this->getAttribute()->getName()); $uploader->setAllowedExtensions($uploadedFile->getData('allowed_extensions')); $uploader->setAllowRenameFiles(true); $uploader->setFilesDispersion(true); $uploader->save(Mage::getBaseDir('media') . '/catalog/product'); } catch (Exception $e) { return $this; } $fileName = $uploader->getUploadedFileName(); if ($fileName) { $object->setData($this->getAttribute()->getName(), $fileName); $this->getAttribute()->getEntity()->saveAttribute($object, $this->getAttribute()->getName()); } return $this; }
public function saveFlag(Varien_Event_Observer $observer) { $store = $observer->getEvent()->getStore(); $data = Mage::app()->getRequest()->getPost(); if (!empty($_FILES)) { if (isset($_FILES['flag']['name']) && $_FILES['flag']['name'] != '') { try { $uploader = new Mage_Core_Model_File_Uploader('flag'); $uploader->setAllowedExtensions(array('jpg', 'jpeg', 'gif', 'png', 'svg')); $uploader->setAllowRenameFiles(true); $uploader->setFilesDispersion(false); $dirPath = Mage::getBaseDir('media') . DS . 'store_flag'; $result = $uploader->save($dirPath, $_FILES['flag']['name']); } catch (Exception $e) { Mage::log($e->getMessage()); } $store->setFlag('store_flag' . $result['file']); } elseif (isset($data['flag']) && is_array($data['flag'])) { if (isset($data['flag']['delete']) && $data['flag']['delete'] === "1") { $store->setFlag(null); } else { $store->setFlag($data['flag']['value']); } } } }
/** * Upload file controller action */ public function uploadAction() { $type = $this->getRequest()->getParam('type'); $tmpPath = ''; if ($type == 'samples') { $tmpPath = Mage_Downloadable_Model_Sample::getBaseTmpPath(); } elseif ($type == 'links') { $tmpPath = Mage_Downloadable_Model_Link::getBaseTmpPath(); } elseif ($type == 'link_samples') { $tmpPath = Mage_Downloadable_Model_Link::getBaseSampleTmpPath(); } $result = array(); try { $uploader = new Mage_Core_Model_File_Uploader($type); $uploader->setAllowRenameFiles(true); $uploader->setFilesDispersion(true); $result = $uploader->save($tmpPath); /** * 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']); if (isset($result['file'])) { $fullPath = rtrim($tmpPath, DS) . DS . ltrim($result['file'], DS); Mage::helper('core/file_storage_database')->saveFile($fullPath); } $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 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 uploadAction() { try { $uploader = new Mage_Core_Model_File_Uploader('file'); $uploader->setAllowedExtensions(); $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['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)); }
/** * Upload file controller action */ public function uploadAction() { $type = $this->getRequest()->getParam('type'); $tmpPath = ''; if ($type == 'samples') { $tmpPath = Mage_Downloadable_Model_Sample::getBaseTmpPath(); } elseif ($type == 'links') { $tmpPath = Mage_Downloadable_Model_Link::getBaseTmpPath(); } elseif ($type == 'link_samples') { $tmpPath = Mage_Downloadable_Model_Link::getBaseSampleTmpPath(); } $result = array(); try { $uploader = new Mage_Core_Model_File_Uploader($type); $uploader->setAllowRenameFiles(true); $uploader->setFilesDispersion(true); $result = $uploader->save($tmpPath); if (isset($result['file'])) { $fullPath = rtrim($tmpPath, DS) . DS . ltrim($result['file'], DS); Mage::helper('Mage_Core_Helper_File_Storage_Database')->saveFile($fullPath); } $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)); }
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; }
public function saveAction() { // check if data sent if ($data = $this->getRequest()->getPost()) { if (!empty($_FILES)) { foreach ($_FILES as $name => $fileData) { if (isset($fileData['name']) && $fileData['name'] != '') { try { $uploader = new Mage_Core_Model_File_Uploader($name); $uploader->setAllowedExtensions(array('jpg', 'jpeg', 'gif', 'png', 'svg')); $uploader->setAllowRenameFiles(true); $uploader->setFilesDispersion(false); $dirPath = Mage::getBaseDir('media') . DS . 'block' . DS; $result = $uploader->save($dirPath, $fileData['name']); } catch (Exception $e) { Mage::log($e->getMessage()); } $data[$name] = 'block/' . $result['file']; } elseif (isset($data[$name]) && is_array($data[$name])) { $data[$name] = $data[$name]['value']; } } } $data = $this->_filterDates($data, array('active_from', 'active_to')); //init model and set data $model = Mage::getModel('block/block'); $model->setData($data); // try to save it try { // save the data $model->save(); // display success message Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('block')->__('The block has been saved.')); // clear previously saved data from session Mage::getSingleton('adminhtml/session')->setFormData(false); // check if 'Save and Continue' if ($this->getRequest()->getParam('back')) { $this->_redirect('*/*/edit', array('block_id' => $model->getId())); return; } // go to grid $this->_redirect('*/*/'); return; } catch (Mage_Core_Exception $e) { $this->_getSession()->addError($e->getMessage()); } catch (Exception $e) { $this->_getSession()->addException($e, Mage::helper('block')->__('An error occurred while saving the block.')); } $this->_getSession()->setFormData($data); $this->_redirect('*/*/edit', array('block_id' => $this->getRequest()->getParam('block_id'))); return; } $this->_redirect('*/*/'); }
public function saveCustomOptionImages(Varien_Event_Observer $observer) { if (!isset($_FILES) || empty($_FILES) || !isset($_FILES['product'])) { return; } $product = $observer->getEvent()->getProduct(); $productData = $observer->getEvent()->getRequest()->getPost('product'); if (isset($productData['options']) && !$product->getOptionsReadonly()) { if (isset($_FILES['product']['name']['options'])) { $images = array(); foreach ($_FILES['product'] as $attr => $options) { if (isset($options['options'])) { foreach ($options['options'] as $optionId => $values) { if (isset($values['values'])) { foreach ($values['values'] as $valueId => $data) { $key = 'option_' . $optionId . '_value_' . $valueId; if (!isset($images[$key])) { $images[$key] = array(); } $images[$key][$attr] = $data['image']; } } } } } foreach ($images as $imageName => $imageData) { $_FILES[$imageName] = $imageData; } } foreach ($productData['options'] as $optionId => $option) { if (!empty($option['values'])) { foreach ($option['values'] as $valueId => $value) { $imageName = 'option_' . $optionId . '_value_' . $valueId; if (!isset($_FILES[$imageName]) || empty($_FILES[$imageName]) || $_FILES[$imageName]['name'] === "") { continue; } try { $uploader = new Mage_Core_Model_File_Uploader($imageName); $uploader->setAllowedExtensions(array('jpg', 'jpeg', 'gif', 'png')); $uploader->setAllowRenameFiles(true); $uploader->setFilesDispersion(false); $dirPath = Mage::getBaseDir('media') . DS . 'custom_option_image' . DS; $result = $uploader->save($dirPath, $_FILES[$imageName]['name']); } catch (Exception $e) { Mage::log($e->getMessage()); } $productData['options'][$optionId]['values'][$valueId]['image'] = 'custom_option_image/' . $result['file']; $product->setCanSaveCustomOptions(true); } } } $product->setProductOptions($productData['options']); } }
public function loadAction() { $request = new Varien_Object($this->getRequest()->getParams()); if ($request && $request->getKey()) { $uploader = new Mage_Core_Model_File_Uploader('file'); $allowed = Mage::getSingleton('cms/wysiwyg_images_storage')->getAllowedExtensions('image'); $uploader->setAllowedExtensions($allowed); $uploader->setAllowRenameFiles(true); $uploader->setFilesDispersion(false); $result = $uploader->save(Mage::helper('cms/wysiwyg_images')->getCurrentPath()); $imageUrl = sprintf('/media/%s/%s', Mage_Cms_Model_Wysiwyg_Config::IMAGE_DIRECTORY, $result['file']); $array = array('filelink' => $imageUrl, 'filename' => $_FILES['file']['name']); echo stripslashes(json_encode($array)); exit; } }
public function saveCurrencyFlags(Varien_Event_Observer $observer) { $currencyFlags = unserialize(Mage::getStoreConfig('currency/options/currencyflag', "")); $deleteFlags = Mage::app()->getRequest()->getParam('delete_currency_flag', null); if (!empty($deleteFlags)) { foreach ($deleteFlags as $currency => $delete) { unset($_FILES[$currency], $currencyFlags[$currency]); } } $fileUpload = false; foreach ($_FILES as $image) { if (isset($image['name']) && $image['name'] !== '') { $fileUpload = true; } } if ($fileUpload) { foreach ($_FILES as $currency => $image) { if (isset($image['name']) && $image['name'] != '') { try { $uploader = new Mage_Core_Model_File_Uploader($currency); $uploader->setAllowedExtensions(array('jpg', 'jpeg', 'gif', 'png', 'svg')); $uploader->setAllowRenameFiles(true); $uploader->setFilesDispersion(false); $dirPath = Mage::getBaseDir('media') . DS . 'currencyflag' . DS; $result = $uploader->save($dirPath, $image['name']); $currencyFlags[$currency] = 'currencyflag' . DS . $result['file']; } catch (Exception $e) { Mage::getSingleton('adminhtml/session')->addError($e->getMessage()); } } } } if (!empty($currencyFlags)) { $config = array('value' => serialize($currencyFlags)); } else { $config = array('inherit' => 1); } Mage::getModel('adminhtml/config_data')->setSection('currency')->setWebsite(null)->setStore(null)->setGroups(array('options' => array('fields' => array('currencyflag' => $config))))->save(); Mage::getSingleton('connect/session')->addSuccess(Mage::helper('adminhtml')->__('Custom currency flags were applied successfully.')); }
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()); /** * 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'; $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 saveAction() { if ($data = $this->getRequest()->getPost()) { if (isset($_FILES['image']['name']) && $_FILES['image']['name'] != '') { try { $uploader = new Mage_Core_Model_File_Uploader('image'); $uploader->setAllowedExtensions(array('jpg', 'jpeg', 'gif', 'png')); $uploader->setAllowRenameFiles(true); $uploader->setFilesDispersion(false); $result = $uploader->save(Mage::getBaseDir('media') . DS . 'testimonials' . DS, $_FILES['image']['name']); } catch (Exception $e) { } $data['image'] = 'testimonials/' . $result['file']; } elseif (is_array($data['image'])) { $data['image'] = $data['image']['value']; } $model = Mage::getModel('testimonials/testimonials'); $model->setData($data)->setId($this->getRequest()->getParam('id')); try { $model->save(); Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('testimonials')->__('Item was successfully saved.')); Mage::getSingleton('adminhtml/session')->setFormData(false); if ($this->getRequest()->getParam('back')) { $this->_redirect('*/*/edit', array('id' => $model->getId())); return; } $this->_redirect('*/*/'); return; } catch (Exception $e) { Mage::getSingleton('adminhtml/session')->addError($e->getMessage()); Mage::getSingleton('adminhtml/session')->setFormData($data); $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id'))); return; } } Mage::getSingleton('adminhtml/session')->addError(Mage::helper('testimonials')->__('Unable to find item to save')); $this->_redirect('*/*/'); }
public function saveAttachment($proofId = false) { $name = 'attachment'; if ($proofId) { $name .= '_' . $proofId; } if (isset($_FILES[$name]) && file_exists($_FILES[$name]['tmp_name'])) { try { $uploader = new Mage_Core_Model_File_Uploader($name); $uploader->setAllowedExtensions(array('jpg', 'jpeg', 'gif', 'png', 'eps', 'pdf', 'ai', 'psd')); $uploader->setAllowRenameFiles(true); $uploader->setFilesDispersion(false); $dirPath = Mage::getBaseDir('media') . DS . 'printproof' . DS; $result = $uploader->save($dirPath, $_FILES[$name]['name']); return 'printproof/' . $result['file']; } catch (Exception $e) { Mage::getSingleton('adminhtml/session')->addError($e->getTraceAsString()); Mage::getSingleton('adminhtml/session')->addError($e->getMessage()); return false; } } return false; }
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); } catch (Exception $e) { return $this; } $uploader->save(Mage::getStoreConfig('system/filesystem/media') . '/catalog/product'); if ($fileName = $uploader->getUploadedFileName()) { $object->setData($this->getAttribute()->getName(), $fileName); $this->getAttribute()->getEntity()->saveAttribute($object, $this->getAttribute()->getName()); } return $this; }
/** * Attach uploaded image to wrapping * * @param string $imageFieldName * @param bool $isTemporary * @return Enterprise_GiftWrapping_Model_Wrapping */ public function attachUploadedImage($imageFieldName, $isTemporary = false) { $isUploaded = true; try { $uploader = new Mage_Core_Model_File_Uploader($imageFieldName); $uploader->setAllowedExtensions(array('jpg', 'jpeg', 'gif', 'png')); $uploader->setAllowRenameFiles(true); $uploader->setAllowCreateFolders(true); $uploader->setFilesDispersion(false); } catch (Exception $e) { $isUploaded = false; } if ($isUploaded) { if ($isTemporary) { $this->setTmpImage($uploader); } else { $this->setImage($uploader); } } 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)); }
/* 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) { $result = array('error' => $e->getMessage(), 'errorcode' => $e->getCode()); }
/** * Upload and resize new file * * @param string $targetPath Target directory * @param string $type Type of storage, e.g. image, media etc. * @throws Mage_Core_Exception * @return array File info Array */ public function uploadFile($targetPath, $type = null) { $uploader = new Mage_Core_Model_File_Uploader('image'); if ($allowed = $this->getAllowedExtensions($type)) { $uploader->setAllowedExtensions($allowed); } $uploader->setAllowRenameFiles(true); $uploader->setFilesDispersion(false); $result = $uploader->save($targetPath); if (!$result) { Mage::throwException(Mage::helper('cms')->__('Cannot upload file.')); } // create thumbnail $this->resizeFile($targetPath . DS . $uploader->getUploadedFileName(), true); $result['cookie'] = array('name' => session_name(), 'value' => $this->getSession()->getSessionId(), 'lifetime' => $this->getSession()->getCookieLifetime(), 'path' => $this->getSession()->getCookiePath(), 'domain' => $this->getSession()->getCookieDomain()); return $result; }
/** * Upload new file * * @param string $targetPath Target directory * @throws Mage_Core_Exception * @return array File info Array */ public function uploadFile($targetPath) { try { $imageName = $_FILES['store_logo']['name']; //file name $uploader = new Mage_Core_Model_File_Uploader('store_logo'); //load class $uploader->setAllowedExtensions(array('jpg', 'jpeg', 'gif', 'png')); //Allowed extension for file $uploader->setAllowCreateFolders(true); //for creating the directory if not exists $uploader->setAllowRenameFiles(true); //if true, uploaded file's name will be changed, if file with the same name already exists directory. $uploader->setFilesDispersion(false); $result = $uploader->save($targetPath, $imageName); //save the file on the specified path if (!$result) { Mage::throwException(Mage::helper('bluehorse_storelocator')->__('Cannot upload file.')); } return $result; } catch (Exception $e) { $this->_getSession()->addException($e, $e->getMessage()); } }
/** * Export attribute value to entity model * * @param Mage_Core_Model_Abstract $entity * @param array|string $value * @return Mage_Customer_Model_Attribute_Data_File */ public function compactValue($value) { if ($this->getIsAjaxRequest()) { return $this; } $attribute = $this->getAttribute(); $original = $this->getEntity()->getData($attribute->getAttributeCode()); $toDelete = false; if ($original) { if (!$attribute->getIsRequired() && !empty($value['delete'])) { $toDelete = true; } if (!empty($value['tmp_name'])) { $toDelete = true; } } $ioFile = new Varien_Io_File(); $path = Mage::getBaseDir('media') . DS . 'customer'; $ioFile->open(array('path' => $path)); // unlink entity file if ($toDelete) { $this->getEntity()->setData($attribute->getAttributeCode(), ''); $file = $path . $original; if ($ioFile->fileExists($file)) { $ioFile->rm($file); } } if (!empty($value['tmp_name'])) { try { $uploader = new Mage_Core_Model_File_Uploader($value); $uploader->setFilesDispersion(true); $uploader->setFilenamesCaseSensitivity(false); $uploader->setAllowRenameFiles(true); $uploader->save($path, $value['name']); $fileName = $uploader->getUploadedFileName(); $this->getEntity()->setData($attribute->getAttributeCode(), $fileName); } catch (Exception $e) { Mage::logException($e); } } return $this; }
/** * Send ReportBug */ public function sendreportBugAction() { $data = $this->getRequest()->getParams(); $path = Mage::getBaseDir('var') . DS . "vdebug"; $attachments = array(); if (isset($data['attachment'])) { $attachments[] = $path . DS . $data['attachment']; } if (isset($_FILES['attachment']) && $_FILES['attachment']['name'][0] != '') { $i = 0; foreach ($_FILES['attachment']['name'] as $file) { if (!isset($_FILES['attachment']['name'][$i]) || $_FILES['attachment']['name'][$i] == "") { continue; } try { $fileName = $file; $fileExt = strtolower(substr(strrchr($fileName, "."), 1)); $fileNamewoe = rtrim($fileName, $fileExt); $fileName = preg_replace('/\\s+', '', $fileNamewoe) . time() . $i . '.' . $fileExt; $uploads = array('name' => $_FILES['attachment']['name'][$i], 'type' => $_FILES['attachment']['type'][$i], 'tmp_name' => $_FILES['attachment']['tmp_name'][$i], 'error' => $_FILES['attachment']['error'][$i], 'size' => $_FILES['attachment']['size'][$i]); $uploader = new Mage_Core_Model_File_Uploader($uploads); $uploader->setAllowedExtensions(array('doc', 'docx', 'pdf', 'jpg', 'png', 'zip')); $uploader->setAllowRenameFiles(false); $uploader->setFilesDispersion(false); if (!is_dir($path)) { mkdir($path, 0777, true); } $uploader->save($path . DS, $fileName); $attachments[] = $path . DS . $fileName; } catch (Exception $e) { Mage::getSingleton('customer/session')->addError($e->getMessage()); //echo "error : ".$e->getMessage(); $error = true; } $i++; } } Mage::helper('csmarketplace/vdebugmail')->sendVdebugReportEmail($data, $attachments); Mage::getSingleton('core/session')->addSuccess("Debug report has been sent."); $this->_redirectReferer(); }
/** * Save action * * @return void */ public function saveAction() { $event = Mage::getModel('enterprise_catalogevent/event')->setStoreId($this->getRequest()->getParam('store', 0)); /* @var $event Enterprise_CatalogEvent_Model_Event */ if ($eventId = $this->getRequest()->getParam('id', false)) { $event->load($eventId); } else { $event->setCategoryId($this->getRequest()->getParam('category_id')); } $postData = $this->_filterPostData($this->getRequest()->getPost()); if (!isset($postData['catalogevent'])) { $this->_getSession()->addError(Mage::helper('enterprise_catalogevent')->__('An error occurred while saving this event.')); $this->_redirect('*/*/edit', array('_current' => true)); return; } $data = new Varien_Object($postData['catalogevent']); $event->setDisplayState($data->getDisplayState())->setStoreDateStart($data->getDateStart())->setStoreDateEnd($data->getDateEnd())->setSortOrder($data->getSortOrder()); $isUploaded = true; try { $uploader = new Mage_Core_Model_File_Uploader('image'); $uploader->setAllowedExtensions(array('jpg', 'jpeg', 'gif', 'png')); $uploader->setAllowRenameFiles(true); $uploader->setAllowCreateFolders(true); $uploader->setFilesDispersion(false); } catch (Exception $e) { $isUploaded = false; } $validateResult = $event->validate(); if ($validateResult !== true) { foreach ($validateResult as $errorMessage) { $this->_getSession()->addError($errorMessage); } $this->_getSession()->setEventData($event->getData()); $this->_redirect('*/*/edit', array('_current' => true)); return; } try { if ($data->getData('image/is_default')) { $event->setImage(null); } elseif ($data->getData('image/delete')) { $event->setImage(''); } elseif ($isUploaded) { try { $event->setImage($uploader); } catch (Exception $e) { Mage::throwException(Mage::helper('enterprise_catalogevent')->__('Image was not uploaded.')); } } $event->save(); $this->_getSession()->addSuccess(Mage::helper('enterprise_catalogevent')->__('Event has been saved.')); if ($this->getRequest()->getParam('_continue')) { $this->_redirect('*/*/edit', array('_current' => true, 'id' => $event->getId())); } else { $this->_redirect('*/*/'); } } catch (Exception $e) { $this->_getSession()->addError($e->getMessage()); $this->_getSession()->setEventData($event->getData()); $this->_redirect('*/*/edit', array('_current' => true)); } }