我已经使用图片上传属性在magento中创建了自定义模块。图像名称已成功存储在DB表中。但是图像不会上传到目标文件夹。
我尝试过以下代码。请告诉我“我在这里做错了什么”
$uploader = new Varien_File_Uploader('fileinputname');
$uploader->setAllowedExtensions(array('jpg','jpeg','gif','png'));
$uploader->setAllowRenameFiles(false);
$uploader->setFilesDispersion(false);
$path = Mage::getBaseDir('media') .DS. 'foo'.DS ;
$uploader->save($path, $_FILES['fileinputname']['name']);
答案 0 :(得分:1)
上周我为我的Magento app添加了文件上传选项。这是代码。这可能对你有所帮助。这是控制器的保存操作。
public function saveAction() {
$filedata = array();
$uploadedFile = 'uploaded_file';
$_helper = Mage::helper('fileuploader');
if (!empty($_FILES[$uploadedFile]['name'])) {
try {
$ext = $_helper->getFileExtension($_FILES[$uploadedFile]['name']);
$fname = 'File-' . time() . $ext;
$uploader = new Varien_File_Uploader($uploadedFile);
#$uploader->setAllowedExtensions(array("txt", "csv", "htm", "html", "xml", "css", "doc", "docx", "xls", "xlsx", "rtf", "ppt", "pdf", "swf", "flv", "avi", "wmv", "mov", "wav", "mp3", "jpg", "jpeg", "gif", "png","zip"));
$uploader->setAllowRenameFiles(true);
$uploader->setFilesDispersion(false);
$path = Mage::getBaseDir('media') . DS . 'custom' . DS . 'upload' . DS;
$uploader->save($path, $fname);
$filedata[$uploadedFile] = 'custom/upload/' . $fname;
} catch (Exception $e) {
Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
$this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
return;
}
}
if ($data = $this->getRequest()->getPost()) {
$products = array();
$availProductIds = Mage::getModel('fileuploader/fileuploader')->getAllAvailProductIds();
parse_str($data['fileuploader_products'], $products);
foreach ($products as $k => $v) {
if (preg_match('/[^0-9]+/', $k) || preg_match('/[^0-9]+/', $v)) {
unset($products[$k]);
}
}
$productIds = array_intersect($availProductIds, $products);
$data['product_ids'] = implode(',', $productIds);
if (!empty($filedata[$uploadedFile])) {
$data[$uploadedFile] = $filedata[$uploadedFile];
} else {
if (isset($data[$uploadedFile]['delete']) && $data[$uploadedFile]['delete'] == 1) {
if ($data[$uploadedFile]['value'] != '')
$this->removeFile($data[$uploadedFile]['value']);
$data[$uploadedFile] = '';
}else {
unset($data[$uploadedFile]);
}
}
$model = Mage::getModel('fileuploader/fileuploader');
$model->setData($data)
->setId($this->getRequest()->getParam('id'));
try {
$model->save();
Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('fileuploader')->__('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('fileuploader')->__('Unable to find item to save'));
$this->_redirect('*/*/');
}
通过在grid.php中添加colomn函数来构建表单
$this->addColumn('uploaded_file', array(
'header' => Mage::helper('fileuploader')->__('File'),
'align' => 'left',
'type' => 'file',
'escape' => true,
'sortable' => false,
'index' => 'uploaded_file',
));
这是最重要的。别忘了提到你的表单enctype应该是form.php文件中的Multi / part。我想你错过了。请检查一下。
protected function _prepareForm() {
$form = new Varien_Data_Form(array(
'id' => 'edit_form',
'action' => $this->getUrl('*/*/save', array('id' => $this->getRequest()->getParam('id'))),
'method' => 'post',
'enctype' => 'multipart/form-data'
)
);
多数民众赞成...... !!
答案 1 :(得分:0)
嗨Elavarasan我似乎无法弄清楚我做了什么,或者我是否真的做了什么。我使用了你建议的magento-module-creator,然后添加了你在helper / data.php下给出的所有代码。什么都没发生。感谢你的帮助,因为我是magento的新人。