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)); }
public function loadFile() { if (!$_FILES) { ?> <form method="POST" enctype="multipart/form-data"> File to upload: <input type="file" name="io_file"/> <input type="submit" value="Upload"/> </form> <?php exit; } if (!empty($_FILES['io_file']['tmp_name'])) { $uploader = new Mage_Core_Model_File_Uploader('io_file'); $uploader->setAllowedExtensions(array('csv', 'xml')); $path = Mage::app()->getConfig()->getTempVarDir() . '/import/'; $uploader->save($path); if ($uploadFile = $uploader->getUploadedFileName()) { $session = Mage::getModel('dataflow/session'); $session->setCreatedDate(date('Y-m-d H:i:s')); $session->setDirection('import'); $session->setUserId(Mage::getSingleton('admin/session')->getUser()->getId()); $session->save(); $sessionId = $session->getId(); $newFilename = 'import_' . $sessionId . '_' . $uploadFile; rename($path . $uploadFile, $path . $newFilename); $session->setFile($newFilename); $session->save(); $this->setData(file_get_contents($path . $newFilename)); Mage::register('current_dataflow_session_id', $sessionId); } } return $this; }
/** * Save uploaded file before saving config value * * @return Mage_Adminhtml_Model_System_Config_Backend_File */ protected function _beforeSave() { $value = $this->getValue(); if (is_array($value) && !empty($value['delete'])) { $this->setValue(''); } 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); $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); } } 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); } }
/** * 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)); }
/** * 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 and set its name to category * * @param Varien_Object $object * @return Mage_Catalog_Model_Category_Attribute_Backend_Image */ public function afterSave($object) { $value = $object->getData($this->getAttribute()->getName()); // if no image was set - nothing to do if (empty($value) && empty($_FILES)) { return $this; } if (is_array($value) && !empty($value['delete'])) { $object->setData($this->getAttribute()->getName(), ''); $this->getAttribute()->getEntity()->saveAttribute($object, $this->getAttribute()->getName()); return $this; } $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); $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 $this; }
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)); }
/** * 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)); }
/** * 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; }
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']); } } } }
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 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 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 saveAction() { $data = $this->getRequest()->getParams(); $uploader = new Mage_Core_Model_File_Uploader('file'); $uploader->setAllowedExtensions(array('csv')); $uploader->setAllowRenameFiles(true); $path = Mage::getBaseDir('var') . DS . 'import'; if (!file_exists($path)) { mkdir($path, 0777); } try { $result = $uploader->save($path); $fullPath = $result['path'] . DS . $result['file']; $csv = new Varien_File_Csv(); $data = $csv->getData($fullPath); $items = array(); if (count($data) > 1) { for ($i = 1; $i < count($data); $i++) { $item = array(); for ($j = 0; $j < count($data[0]); $j++) { if (isset($data[$i][$j]) && trim($data[$i][$j]) != '') { $item[strtolower($data[0][$j])] = $data[$i][$j]; } } $items[] = $item; } } $resource = Mage::getSingleton('core/resource'); $writeConnection = $resource->getConnection('core_write'); $table = $resource->getTableName('seo/redirect'); $table2 = $resource->getTableName('seo/redirect_store'); $i = 0; foreach ($items as $item) { pr($item); if (!isset($item['url_from']) || !isset($item['url_to'])) { continue; } $item = new Varien_Object($item); $query = "REPLACE {$table} SET\n url_from = '" . addslashes($item->getUrlFrom()) . "',\n url_to = '" . addslashes($item->getUrlTo()) . "',\n is_redirect_only_error_page = '" . addslashes($item->getIsRedirectOnlyErrorPage()) . "',\n comments = '" . addslashes($item->getComments()) . "',\n is_active = '" . addslashes($item->getIsActive()) . "';\n REPLACE {$table2} SET\n store_id = 0,\n redirect_id = LAST_INSERT_ID();\n "; $writeConnection->query($query); $i++; } Mage::getSingleton('adminhtml/session')->addSuccess('' . $i . ' records were inserted or updated'); } catch (Exception $e) { Mage::getSingleton('adminhtml/session')->addError($e->getMessage()); } $this->_redirect('*/*/'); }
protected function _moveFile($tmpPath, $destPath) { if (!Mage::getStoreConfigFlag('magefm_cdn/general/enabled')) { return parent::_moveFile($tmpPath, $destPath); } return Mage::helper('magefm_cdn/storage')->saveFileFromPath($tmpPath, $destPath); }
/** * Does some saving action * * @param [type] $name name of the input field * * @return [string | false] filename or false on exception */ public function saveImage($name) { $path = $this->getFullImagesDir(); try { $uploader = new Mage_Core_Model_File_Uploader($name); $uploader->setAllowedExtensions(array('jpg', 'jpeg', 'gif', 'png')); $uploader->setAllowRenameFiles(true); $result = $uploader->save($path); return $result['file']; } catch (Exception $e) { if ($e->getCode() != Mage_Core_Model_File_Uploader::TMP_NAME_EMPTY) { Mage::logException($e); } return; } }
/** * Count files recursively for given directory * * @param string $directory * @param array $files * @param int $level * @param string $sku * * @return array */ public function getFiles($directory, $files = array(), $level = 0, $sku = '') { $level++; $handle = opendir($directory); while ($file = readdir($handle)) { if ($file != "." && $file != ".." && $file != ".DS_Store") { if (is_dir($directory . DS . $file)) { if ($level == 1) { $sku = $file; $files[$sku] = array(); } $files = $this->getFiles($directory . DS . $file, $files, $level, $sku); if (isset($files[$sku])) { if (!count($files[$sku])) { unset($files[$sku]); } } } else { $extension = pathinfo($directory . DS . $file, PATHINFO_EXTENSION); $name = strtolower($sku . '-' . (count($files[$sku]) + 1) . '.' . $extension); $fileName = Mage_Core_Model_File_Uploader::getCorrectFileName($name); $path = Mage_Core_Model_File_Uploader::getDispretionPath($fileName); $fileName = $path . DS . $fileName; if (empty($files[$sku][$directory])) { $files[$sku][$directory] = array('directory' => $directory . DS, 'file' => $file, 'name' => $fileName); } ksort($files[$sku]); } } } closedir($handle); return $files; }
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 saveAction() { $data = $this->getRequest()->getParams(); $uploader = new Mage_Core_Model_File_Uploader('file'); $uploader->setAllowedExtensions(array('csv')); $uploader->setAllowRenameFiles(true); $path = Mage::getBaseDir('var') . DS . 'import'; if (!file_exists($path)) { mkdir($path, 0777); } try { $result = $uploader->save($path); $fullPath = $result['path'] . DS . $result['file']; $csv = new Varien_File_Csv(); $data = $csv->getData($fullPath); $items = array(); if (count($data) > 1) { for ($i = 1; $i < count($data); $i++) { $item = array(); for ($j = 0; $j < count($data[0]); $j++) { if (isset($data[$i][$j]) && trim($data[$i][$j]) != '') { $item[strtolower($data[0][$j])] = $data[$i][$j]; } } $items[] = $item; } } $resource = Mage::getSingleton('core/resource'); $writeConnection = $resource->getConnection('core_write'); $table = $resource->getTableName('seoautolink/link'); $table2 = $resource->getTableName('seoautolink/link_store'); $i = 0; foreach ($items as $item) { if (!isset($item['keyword'])) { continue; } $item = new Varien_Object($item); $query = "REPLACE {$table} SET\n keyword = '" . addslashes($item->getKeyword()) . "',\n url = '" . addslashes($item->getUrl()) . "',\n url_title = '" . addslashes($item->getUrlTitle()) . "',\n url_target = '" . addslashes($item->getUrlTarget()) . "',\n is_nofollow = '" . (int) $item->getIsNofollow() . "',\n max_replacements = '" . (int) $item->getMaxReplacements() . "',\n sort_order = '" . (int) $item->getSortOrder() . "',\n occurence = '" . (int) $item->getOccurence() . "',\n is_active = '" . (int) $item->getIsActive() . "',\n created_at = '" . now() . "',\n updated_at = '" . now() . "';\n REPLACE {$table2} SET\n store_id = '" . (int) $item->getStoreId() . "',\n link_id = LAST_INSERT_ID();\n "; $writeConnection->query($query); $i++; } Mage::getSingleton('adminhtml/session')->addSuccess('' . $i . ' records were inserted or updated'); } catch (Exception $e) { Mage::getSingleton('adminhtml/session')->addError($e->getMessage()); } $this->_redirect('*/*/'); }
/** * 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; }
public function _afterSave() { if (empty($_FILES['groups']['tmp_name']['purchaseorder']['fields']['paid_icon']['value'])) { return $this; } try { $_FILES['paid_icon'] = array('name' => $_FILES['groups']['name']['purchaseorder']['fields']['paid_icon']['value'], 'type' => $_FILES['groups']['type']['purchaseorder']['fields']['paid_icon']['value'], 'tmp_name' => $_FILES['groups']['tmp_name']['purchaseorder']['fields']['paid_icon']['value'], 'error' => $_FILES['groups']['error']['purchaseorder']['fields']['paid_icon']['value'], 'size' => $_FILES['groups']['size']['purchaseorder']['fields']['paid_icon']['value']); $path = Mage::helper('emjainteractive_purchaseordermanagement')->getIconMediaPath(); $uploader = new Mage_Core_Model_File_Uploader('paid_icon'); $uploader->setAllowedExtensions(array('jpg', 'jpeg', 'gif', 'png')); $uploader->setAllowRenameFiles(false); $result = $uploader->save($path); } catch (Exception $e) { Mage::logException($e); throw new Exception($e); } }
/** * 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; }
/** * 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; }
/** * Add image to media gallery and return new filename * * @param Mage_Catalog_Model_Product $product * @param string $file file path of image in file system * @param string|array $mediaAttribute code of attribute with type 'media_image', * leave blank if image should be only in gallery * @param boolean $move if true, it will move source file * @param boolean $exclude mark image as disabled in product page view * @return string */ public function addImage(Mage_Catalog_Model_Product $product, $file, $mediaAttribute = null, $move = false, $exclude = true) { if (!Mage::helper('uaudio_storage')->isEnabled()) { return parent::addImage(product, $file, $mediaAttribute, $move, $exclude); } $file = realpath($file); if (!$file || !file_exists($file)) { Mage::throwException(Mage::helper('catalog')->__('Image does not exist.')); } Mage::dispatchEvent('catalog_product_media_add_image', array('product' => $product, 'image' => $file)); $pathinfo = pathinfo($file); $imgExtensions = array('jpg', 'jpeg', 'gif', 'png'); if (!isset($pathinfo['extension']) || !in_array(strtolower($pathinfo['extension']), $imgExtensions)) { Mage::throwException(Mage::helper('catalog')->__('Invalid image file type.')); } $fileName = Mage_Core_Model_File_Uploader::getCorrectFileName($pathinfo['basename']); $dispretionPath = Mage_Core_Model_File_Uploader::getDispretionPath($fileName); $fileName = $dispretionPath . DS . $fileName; $dest = $this->_getConfig()->getTmpMediaPath($fileName); $storageModel = Mage::getSingleton('core/file_storage')->getStorageModel(); $storageModel->setAllowRenameFiles(true); try { if ($move) { $uploadDestination = $storageModel->moveFile($file, $dest); } else { $uploadDestination = $storageModel->moveUploadFile($file, $dest); } if (!$uploadDestination) { throw new Exception(); } $fileName = basename($uploadDestination); $dispretionPath = Mage_Core_Model_File_Uploader::getDispretionPath($fileName); $fileName = $dispretionPath . DS . $fileName; } catch (Exception $e) { Mage::throwException(Mage::helper('catalog')->__('Failed to move file: %s', $e->getMessage())); } $fileName = str_replace(DS, '/', $fileName); $attrCode = $this->getAttribute()->getAttributeCode(); $mediaGalleryData = $product->getData($attrCode); $position = 0; if (!is_array($mediaGalleryData)) { $mediaGalleryData = array('images' => array()); } foreach ($mediaGalleryData['images'] as &$image) { if (isset($image['position']) && $image['position'] > $position) { $position = $image['position']; } } $position++; $mediaGalleryData['images'][] = array('file' => $fileName, 'position' => $position, 'label' => '', 'disabled' => (int) $exclude); $product->setData($attrCode, $mediaGalleryData); if (!is_null($mediaAttribute)) { $this->setMediaAttribute($product, $mediaAttribute, $fileName); } return $fileName; }
protected function _saveAttachment($name) { $path = Mage::getBaseDir('media') . '/productattachment/'; if (!file_exists($path)) { mkdir($path); } if (isset($_FILES['product']['name'][$name]) && file_exists($_FILES['product']['tmp_name'][$name])) { try { $uploader = new Mage_Core_Model_File_Uploader($name); $image = $uploader->setAllowedExtensions(array('jpg', 'jpeg', 'gif', 'png', 'pdf', 'eps', 'doc', 'docx', 'csv', 'txt', 'xls', 'psd'))->setAllowRenameFiles(true)->setFilesDispersion(true)->save($path, $_FILES['product']['name'][$name]); return $image['file']; } catch (Exception $e) { Mage::getSingleton('adminhtml/session')->addError($e->getTraceAsString()); Mage::getSingleton('adminhtml/session')->addError($e->getMessage()); return false; } } return false; }
protected function _afterSave($result) { parent::_afterSave($result); if (!empty($result['path']) && !empty($result['file'])) { $imageProvider = CloudinaryImageProvider::fromConfiguration($this->_getConfigHelper()->buildConfiguration()); $imageProvider->upload(Image::fromPath($result['path'] . DIRECTORY_SEPARATOR . $result['file'])); $this->_trackSynchronisation($result['file']); } 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']; $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; }
public function prepareForSave($data) { if (isset($data['delete'])) { $this->_deleteFile = (bool) $data['delete']; if ($this->_deleteFile) { return ''; } } $fileName = !empty($data['value']) ? $data['value'] : $data['existing']; return Mage_Core_Model_File_Uploader::getNewFileName(strtolower($fileName)); }
/** * 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; } }