Magento 模块在管理中上传图像

标签 magento

我正在开发一个具有前端和后端的模块。到目前为止一切都很好,但现在我想在后端上传图片。我不知道如何开始,我尝试过的一切都让我头疼。

谢谢

最佳答案

经过几天的研究,这里有一个易于使用的示例,说明如何在 magento 中上传文件 How to create an image or video uploader for the Magento Admin Panel

基本上,我们需要将 'enctype' => 'multipart/form-data' 添加到我们的表单

$form = new Varien_Data_Form(array(
        'id' => 'edit_form',
        'action' => $this->getUrl('*/*/save'),
        'method' => 'post',
        'enctype' => 'multipart/form-data'
    )
);

file 类型的字段添加到我们的字段集中

$fieldset->addField('fileinputname', 'file', array(
    'label'     => 'File label',
    'required'  => false,
    'name'      => 'fileinputname',
));

并将其保存在我们的 Controller 中

if(isset($_FILES['fileinputname']['name']) and (file_exists($_FILES['fileinputname']['tmp_name']))) {
  try {
    $uploader = new Varien_File_Uploader('fileinputname');
    $uploader->setAllowedExtensions(array('jpg','jpeg','gif','png'));

    $uploader->setAllowRenameFiles(false);

    $path = Mage::getBaseDir('media') . DS ;

    $uploader->save($path, $_FILES['fileinputname']['name']);

    $data['fileinputname'] = $_FILES['fileinputname']['name'];
  }catch(Exception $e) {

  }
}

关于Magento 模块在管理中上传图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3630828/

相关文章:

php - Mysql:查询未执行

php - 需要在 magento 中实时显示用户的选择

php - Magento - 类别在目录中显示不正确

sql - 如何使用 Magento Collections 中的表达式执行 ORDER BY

mysql - 导入大型 SQL 转储时遇到问题

magento - 翻译 Magento 前端

Magento 无法将简单的产品与可配置的产品相关联

php - 检测到服务器端扫描仪 "php.malware.magento-cc-stealer.069"

wordpress - 开设网上商店:我该如何选择?

Magento 产品保存时出现 `catalog_product_entity_tier_price` 错误中的重复条目