【发布时间】:2015-11-26 09:56:58
【问题描述】:
只是我对 magento 管理网格研究的继续。我正在尝试创建文件上传。保存过程已完成,没有错误,并且在数据库中添加了一个表格行。
问题:
文件路径未保存在数据库中。
没有创建目录路径,当然也没有文件上传到服务器。
注意:该过程在网格和数据库中添加了额外的表格行。
我只是参考这个链接Magento image upload form field
问题:
如何为我上传的文件创建目录文件夹?
如何在数据库表中保存路径?
config.xml中有代码要加吗?
更新
终于解决了文件上传问题。 感谢@PHP Weblineindia 指导我。 这是我更新的控制器。
public function saveAction() {
$post_data=$this->getRequest()->getPost();
if ($post_data) {
try {
//save file to the destination folder
if (isset($_FILES)){
if ($_FILES['file_path']['name']) {
$path = Mage::getBaseDir('media') . DS . 'rts' . DS .'pmadmin'.DS;
$uploader = new Varien_File_Uploader('file_path');
$uploader->setAllowedExtensions(array('PDF','pdf'));
$uploader->setAllowRenameFiles(false);
$uploader->setFilesDispersion(false);
$destFile = $path.$_FILES['file_path']['name'];
$filename = $uploader->getNewFileName($destFile);
$uploader->save($path, $filename);
$post_data['file_path']='rts/pmadmin/'.$filename;
}
}
//save file path to the database
$model = Mage::getModel("pmadmin/pmadmin")
->addData($post_data)
->setId($this->getRequest()->getParam("id"))
->save();
Mage::getSingleton("adminhtml/session")->addSuccess(Mage::helper("adminhtml")->__("File was successfully saved"));
Mage::getSingleton("adminhtml/session")->setPmadminData(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")->setPmadminData($this->getRequest()->getPost());
$this->_redirect("*/*/edit", array("id" => $this->getRequest()->getParam("id")));
return;
}
}
$this->_redirect("*/*/");
}
这是我的表格。
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'
)
);
$form->setUseContainer(true);
$this->setForm($form);
return parent::_prepareForm();
}
路径保存在表的file_path列,文件上传到media/pmadmin目录。
谢谢!
【问题讨论】: