2010-09-02 85 views
9

我正在開發一個模塊,同時具有前端和後端。到現在爲止一切都好,但現在我想在後端上傳圖片。我不知道該如何開始,而我所嘗試過的一切都讓我頭痛。Magento模塊上傳管理圖像

感謝

回答

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

    } 
} 
+0

只是美好的,拯救了我的一天 – Yaroslav 2012-11-15 15:34:23

+0

要麼我在那個上做錯了什麼,要麼它不適用於1.4。 – Mike 2013-05-17 17:20:51

+0

這是在1.9上進行測試的,但是你可以試試用1.6發佈的CE。 – 2013-07-29 13:00:34

5

這需要進入你的模塊的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> 
+0

我們如何可以調整與上面的代碼的形象? – liyakat 2013-08-22 13:01:49

+0

這個問題太舊了,我甚至都不記得這段代碼是幹什麼的。我建議你創建一個新的問題。 – mattalxndr 2013-08-22 13:20:41

+0

你忘了編碼? :( – liyakat 2013-08-23 04:04:19