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?
Best Answer
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)
If it can help !