Magento模块在管理员中上传图像

时间:2010-09-02 20:11:12

标签: magento

我正在开发一个包含前端和后端的模块。到目前为止一切都还可以,但现在我想在后端上传图像。我不知道如何开始,我尝试的一切都让我头疼。

由于

2 个答案:

答案 0 :(得分:10)

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

Bassically,我们需要将'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',
));

并将其保存在我们的控制器中

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) {

  }
}

答案 1 :(得分:7)

这需要进入模块的etc/system.xml文件:

<?xml version="1.0"?>
<config>
    <sections>
        <imagesection> <!-- Make up a section key (configuration sidebar) -->
            <!-- ... -->
            <groups>
                <imagegroup> <!-- Make up a group key (the part you can expand/collapse) -->
                    <!-- ... -->
                    <fields>
                        <imagefield> <!-- Make up a field key -->
                            <label>Field Name</label>
                            <frontend_type>image</frontend_type>
                            <backend_model>adminhtml/system_config_backend_image</backend_model>
                            <upload_dir config="system/filesystem/media" scope_info="1">uploaddir</upload_dir> <!-- would upload into media/uploaddir -->
                            <base_url type="media" scope_info="1">uploaddir</base_url> <!-- same as previous line -->
                            <sort_order>10</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                        </imagefield>