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; }
/** * 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; }
/** * 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 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; }
public function load() { 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()) { $fp = fopen($uploadFile, 'rb'); while ($row = fgetcsv($fp)) { } fclose($fp); } } return $this; }
/** * 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; }
protected function _afterSave() { if (is_string($this->getGuiData())) { $this->setGuiData(unserialize($this->getGuiData())); } $profileHistory = Mage::getModel('Mage_Dataflow_Model_Profile_History'); $adminUserId = $this->getAdminUserId(); if ($adminUserId) { $profileHistory->setUserId($adminUserId); } $profileHistory->setProfileId($this->getId())->setActionCode($this->getOrigData('profile_id') ? 'update' : 'create')->save(); if (isset($_FILES['file_1']['tmp_name']) || isset($_FILES['file_2']['tmp_name']) || isset($_FILES['file_3']['tmp_name'])) { for ($index = 0; $index < 3; $index++) { if ($file = $_FILES['file_' . ($index + 1)]['tmp_name']) { $uploader = new Mage_Core_Model_File_Uploader('file_' . ($index + 1)); $uploader->setAllowedExtensions(array('csv', 'xml')); $path = Mage::app()->getConfig()->getTempVarDir() . '/import/'; $uploader->save($path); if ($uploadFile = $uploader->getUploadedFileName()) { $newFilename = 'import-' . date('YmdHis') . '-' . ($index + 1) . '_' . $uploadFile; rename($path . $uploadFile, $path . $newFilename); } } //BOM deleting for UTF files if (isset($newFilename) && $newFilename) { $contents = file_get_contents($path . $newFilename); if (ord($contents[0]) == 0xef && ord($contents[1]) == 0xbb && ord($contents[2]) == 0xbf) { $contents = substr($contents, 3); file_put_contents($path . $newFilename, $contents); } unset($contents); } } } parent::_afterSave(); }
/** * 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; }