1.phtml页面
<form action="<?php echo $this->getUrl('*/Sales_Order_Create/upload') ?>" method="post" enctype="multipart/form-data">
<?php echo $this->getBlockHtml('formkey')?><!--form表单这个不能少-->
<input type="file" name="file"/>
<button type="submit" >submit</button>
</form>
2.上面我们把表单提交到了 Sales_Order_Create/upload方法下
public function uploadAction(){
//Mage::log($_FILES);
if ($data = $this->getRequest()->getPost()) {
$type = 'file';//表单中的name
if(isset($_FILES[$type]['name']) && $_FILES[$type]['name'] != '') {
try {
$uploader = new Varien_File_Uploader($type);
$uploader->setAllowedExtensions(array('xlsx','xls'));//限制文件类型
$uploader->setAllowRenameFiles(true);
$uploader->setFilesDispersion(true);
//$path = Mage::getBaseDir('media') . DS . 'uploads' . DS;
$path = Mage::getBaseDir('var') . DS . 'uploads' . DS;//文件保存路径
$uploader->save($path, $_FILES[$type]['name'] );
$filename = $uploader->getUploadedFileName();//返回文件名带目录:/t/p/tpp_price_1.xlsx
} catch (Exception $e) {
Mage::log('excel上传出错:'.$e->getMessage());
}
}
echo $filename;
}
}