我如何从magento的前端上传图片?
How can i upload image from front-end in magento?
我正在尝试从我的自定义模块中的前端上传单个图像。这是我在 test/template/test.phtml
中的表格
<form action="<?php echo Mage::getUrl('text/index/save') ?>" method="post" enctype="multipart/form-data">
<label for="sb_link">Image</label>
<input type="file" id="bs_image" name="bs_image" required="true"/>
<input type="submit" value="Submit" />
这是我的控制器,我在其中上传媒体文件夹中的图像并在数据库中保存名称。
<?php
public function saveAction() {
echo $path = Mage::getBaseDir() . '/test';
if (!file_exists($path)) {
mkdir($path, 777, true);
}
try {
$fname = $_FILES['test']['name'];
$uploader = new Varien_File_Uploader('test');
$uploader->setAllowedExtensions(array('png', 'gif', 'jpeg', 'docx'));
$uploader->setAllowCreateFolders(true);
$uploader->setAllowRenameFiles(false);
$uploader->setFilesDispersion(false);
$uploader->save($path, $fname);
} catch (Exception $e) {
echo 'Error Message: ' . $e->getMessage();
}
$contact = Mage::getModel('test/test');
$contact->setData('bs_image', $bs_image);
$contact->save();
$this->_redirectReferer();
}
我收到此 error.Error 消息:文件未上传。
谢谢
我已经检查了你的代码。在表单中,您使用 bs_image 作为图像字段的名称。但是在控制器中,您将 post 文件作为 $_FILES['test']['name'];。将其更改为 $_FILES['bs_image']['name']; 。
确保您已将结束表单标记放入文件中。
您需要像这样访问文件名
$fname = $_FILES['bs_image']['name'];
我正在尝试从我的自定义模块中的前端上传单个图像。这是我在 test/template/test.phtml
中的表格<form action="<?php echo Mage::getUrl('text/index/save') ?>" method="post" enctype="multipart/form-data">
<label for="sb_link">Image</label>
<input type="file" id="bs_image" name="bs_image" required="true"/>
<input type="submit" value="Submit" />
这是我的控制器,我在其中上传媒体文件夹中的图像并在数据库中保存名称。
<?php
public function saveAction() {
echo $path = Mage::getBaseDir() . '/test';
if (!file_exists($path)) {
mkdir($path, 777, true);
}
try {
$fname = $_FILES['test']['name'];
$uploader = new Varien_File_Uploader('test');
$uploader->setAllowedExtensions(array('png', 'gif', 'jpeg', 'docx'));
$uploader->setAllowCreateFolders(true);
$uploader->setAllowRenameFiles(false);
$uploader->setFilesDispersion(false);
$uploader->save($path, $fname);
} catch (Exception $e) {
echo 'Error Message: ' . $e->getMessage();
}
$contact = Mage::getModel('test/test');
$contact->setData('bs_image', $bs_image);
$contact->save();
$this->_redirectReferer();
}
我收到此 error.Error 消息:文件未上传。 谢谢
我已经检查了你的代码。在表单中,您使用 bs_image 作为图像字段的名称。但是在控制器中,您将 post 文件作为 $_FILES['test']['name'];。将其更改为 $_FILES['bs_image']['name']; 。
确保您已将结束表单标记放入文件中。 您需要像这样访问文件名 $fname = $_FILES['bs_image']['name'];