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

Cas*_*Roy 9 magento

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

谢谢

Cas*_*Roy 10

经过几天的研究,这是一个易于使用的示例,说明如何上传magento中的文件 如何为Magento管理面板创建图像或视频上传器

在Bassically,我们需要添加'enctype' => 'multipart/form-data'到我们的表单

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

在字段file集中 添加类型字段

$fieldset->addField('fileinputname', 'file', array(
    'label'     => 'File label',
    'required'  => false,
    'name'      => 'fileinputname',
));
Run Code Online (Sandbox Code Playgroud)

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

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

  }
}
Run Code Online (Sandbox Code Playgroud)


mat*_*ndr 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>
Run Code Online (Sandbox Code Playgroud)