I try to upload a file using Zend.
I made a standard form with a submit button which calls a controller.
In this controller, I have :
<?php
class Mine_Custoptiontypev5_UpfileController extends Mage_Core_Controller_Front_Action
{
 public function updatefileAction()
 {
 $post = $this->getRequest()->getPost();
 try {
 if (empty($post) || empty($_FILES)) {
 echo 'Invalid form data.';
 return $this;
 }
 $_order_id = $post['order_id'];
 $_item_id = $post['item_id'];
 $_option_id = $post['option_id'];
 $_action = $post['options_'.$_option_id.'_file_action'];
 $upload = new Zend_File_Transfer_Adapter_Http();
 $file = 'options_' . $_option_id . '_file';
 $runValidation = $upload->isUploaded($file);
 if (!$runValidation) {
 return $this;
 }
 $fileInfo = $upload->getFileInfo($file);
 $fileInfo = $fileInfo[$file];
 $fileInfo['title'] = $fileInfo['name']; 
 } 
 catch (Exception $e) {
 // when file exceeds the upload_max_filesize, $_FILES is empty
 if (isset($_SERVER['CONTENT_LENGTH']) && $_SERVER['CONTENT_LENGTH'] > $this->_getUploadMaxFilesize()) {
 $_option->setIsValid(false);
 $value = $this->_bytesToMbytes($this->_getUploadMaxFilesize());
 Mage::throwException(
 Mage::helper('catalog')->__("The file you uploaded is larger than %s Megabytes allowed by server", $value)
 );
 } 
 }
 /**
 * Option Validations
 */
 // Image dimensions
 $_dimentions = array();
 if ($_option->getImageSizeX() > 0) {
 $_dimentions['maxwidth'] = $_option->getImageSizeX();
 }
 if ($_option->getImageSizeY() > 0) {
 $_dimentions['maxheight'] = $_option->getImageSizeY();
 }
 if (count($_dimentions) > 0) {
 $upload->addValidator('ImageSize', false, $_dimentions);
 }
 // File extension
 $_allowed = $this->_parseExtensionsString($_option->getFileExtension());
 if ($_allowed !== null) {
 $upload->addValidator('Extension', false, $_allowed); 
 } 
 else {
 $_forbidden = $this->_parseExtensionsString(Mage::getModel('Mine_Custoptiontypev5_Model_Catalog_Product_Option_Type_Xfiletype')->getConfigData('forbidden_extensions'));
 if ($_forbidden !== null) {
 $upload->addValidator('ExcludeExtension', false, $_forbidden);
 }
 }
 // Maximum filesize
 $upload->addValidator('FilesSize', false, array('max' => $this->_getUploadMaxFilesize()));
 /**
 * Upload process
 */ 
 $this->_initFilesystem();
 if ($upload->isUploaded($file) && $upload->isValid($file)) {
 $extension = pathinfo(strtolower($fileInfo['name']), PATHINFO_EXTENSION);
 $fileName = Mage_Core_Model_File_Uploader::getCorrectFileName($fileInfo['name']);
 $dispersion = Mage_Core_Model_File_Uploader::getDispretionPath($fileName);
 $filePath = $dispersion;
 $fileHash = md5(file_get_contents($fileInfo['tmp_name']));
 $filePath .= DS . $fileHash . '.' . $extension;
 $fileFullPath = $this->getQuoteTargetDir() . $filePath;
 $upload->addFilter('Rename', 
 array(
 'target' => $fileFullPath,
 'overwrite' => true
 ));
 try {
 $upload->receive();
 Zend_Debug::dump($upload->getFileInfo());}
 catch (Zend_File_Transfer_Exception $e) {
 Zend_Debug::dump($e->message());
 }
...
But my file is not moved to the desired destination path (../quote/x/x/xxxxxxxxxxxxx.xxx)
Could you help me please?
- 
 Try This, hope it will helps magento.stackexchange.com/questions/25653/…Charlie– Charlie2014年07月31日 09:57:09 +00:00Commented Jul 31, 2014 at 9:57
2 Answers 2
Thanks to Knase answer, I surched with his method, but the goal was to have the same file upload system as the file product option :
file rename with secretkey and Dispersion based on the original file name
(something like : /order/A/e/zeaiu8certerrtj998n.zip)
$uploader = new Varien_File_Uploader($_filename);
$uploader->setAllowedExtensions(array('zip','ZIP'));
$uploader->setAllowRenameFiles(true); //if true, uploaded file's name will be changed, if file with the same name already exists directory. Necessary to avoid conflicts
$uploader->setFilesDispersion(false); //To have a dispersion based on the original file name (as the file option does), we will have to do it manually
$uploader->setAllowCreateFolders(true); //for creating the directory if not exists
$extension = pathinfo(strtolower($_file['name']), PATHINFO_EXTENSION);
$fileName = Mage_Core_Model_File_Uploader::getCorrectFileName($_file['name']);
$dispersion = Mage_Core_Model_File_Uploader::getDispretionPath($fileName); // We get the dispersion manually here
$fileHash = md5(file_get_contents($_file['tmp_name'])); //here the secretkey
$path = $this->getOrderTargetDir() . $dispersion;
$DestName = $fileHash . '.' . $extension;
$result = $uploader->save($path, $DestName);
If it can help !
To my mind you didnt create sub folders for file. And you can use magento uploader. It creates subfolders. Look class Varien_File_Uploader. Example:
<?php
class Mine_Custoptiontypev5_UpfileController extends Mage_Core_Controller_Front_Action
{
 public function updatefileAction()
 {
 $result = array();
 if ($this->getRequest()->isPost() && !empty($_FILES[$fileName]['name'])) {
 try {
 $uploader = new Varien_File_Uploader($fileName);
 $uploader->setAllowCreateFolders(true)->setFilesDispersion(true);
 $path = Mage::getBaseDir('var') . DS;
 $result = $uploader->save($path);
 } catch (Exception $e) {
 $result['error'] = 'Error upload po document'; 
 }
 return $result
 }
}
- 
 thank you for your answer. That is what I tried to do first, but as it is for a product file option modification, I need a secret key and file rename, and if I do not make mistake, it is a zend stuff...Alexglvr– Alexglvr2014年07月31日 10:04:23 +00:00Commented Jul 31, 2014 at 10:04
- 
 I managed to do it this way !Alexglvr– Alexglvr2014年08月01日 08:07:03 +00:00Commented Aug 1, 2014 at 8:07
Explore related questions
See similar questions with these tags.