2013-04-23 98 views
2

我在這裏有一個問題,我認爲這基本上只是因爲我錯過了一步。當我保存文件時,圖像將保存到指定的目錄中。但是,當我去編輯表單時,我上傳的圖像沒有顯示。Magento Image字段管理網格不保存

這是我建立的第一個網格模塊,所以我有點不清楚它的工作原理。

在form.php的我的圖像字段如下:

$fieldset->addField('something_else', 'image', array(
      'name'  => 'something_else[something_else]', 
      'label'  => 'Something Else', 
      'title'  => 'Something Else', 
      'required' => false, 
     )); 

我已設置加密類型是: '是enctype'=> '的multipart/form-data的'

我的控制器有該代碼在拯救行動,如果後($ POSTDATA = $這個 - > Request()方法 - >的getPost()):

if(isset($_FILES['something_else'])) 
       { 
        foreach($_FILES['something_else']['name'] as $imgname => $img) 
        { 
         if($_FILES['something_else']['name'][$imgname] != '' && isset($_FILES['something_else']['name'][$imgname])) 
         { 
          $imgArray = array(
           'name' => $_FILES['something_else']['name'][$imgname], 
           'type' => $_FILES['something_else']['type'][$imgname], 
           'tmp_name' => $_FILES['something_else']['tmp_name'][$imgname], 
           'error' => $_FILES['something_else']['error'][$imgname], 
           'size' => $_FILES['something_else']['size'][$imgname] 
          ); 
          $uploader = new Varien_File_Uploader($imgArray); 
          $uploader->setAllowedExtensions(array('jpg','jpeg','png','gif')); 
          $uploader->setAllowRenameFiles(true); 
          $uploader->setFilesDispersion(false); 

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

          $name = str_replace(' ', '_', $_FILES['something_else']['name'][$imgname]); 

          $uploader->save($path, $name); 

          $bazData[$imgname] = 'somethingelse' . DS . $uploader->getCorrectFileName($name); 
         } 
         else 
         { 
          unset($bazData[$imgname]); 
         } 
        } 
       } 

有什麼進一步的我缺少的數據發送回form.php的顯示時查看錶單?

我希望這是足夠的信息,並提前致謝。

+0

打印POST數組檢查保存前的圖像存在字段。要麼你得到這個領域或不在後? – 2013-04-23 10:35:26

+0

var_dump image array返回數組(5){[「name」] => string(17)「How-tostyle_2.jpg」[「type」] => string(10)「image/jpeg」[「tmp_name」 ] => string(26)「/ private/var/tmp/phpZqvj12」[「error」] => int(0)[「size」] => int(304120)} – 2013-04-23 12:00:56

回答

0

您必須在表單中設置字段值。像:

$data = array('something_else' => 'image/url'); 
$fieldset->addField('something_else', 'image', array(
      'name'  => 'something_else', 
      'label'  => 'Something Else', 
      'title'  => 'Something Else', 
      'required' => false, 
     )); 
$form->setValues($data); 

或者:

$fieldset->addField('something_else', 'image', array(
      'name'  => 'something_else', 
      'label'  => 'Something Else', 
      'title'  => 'Something Else', 
      'required' => false, 
      'value'  => 'image/url', 
     )); 
1

嘗試的代碼 這個和平的形式像場使用此代碼:

$fieldset->addField('filename', 'file', array(
      'label'  => Mage::helper('test')->__('File'), 
      'required' => false, 
      'name'  => 'filename', 
    )); 

而保存功能可按控制器中使用這種

if(isset($_FILES['filename']['name']) && $_FILES['filename']['name'] != '') { 
      try { 
        /* Starting upload */ 
        $uploader = new Varien_File_Uploader('filename'); 

        // Any extention would work 
        $uploader->setAllowedExtensions(array('jpg','jpeg','gif','png')); 
        $uploader->setAllowRenameFiles(false); 

        // Set the file upload mode 
        // false -> get the file directly in the specified folder 
        // true -> get the file in the product like folders 
        // (file.jpg will go in something like /media/f/i/file.jpg) 
        $uploader->setFilesDispersion(false); 

        // We set media as the upload dir 
        $path = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA)."test/" ; 
        //echo $path, $_FILES['filename']['name'];exit; 
        $uploader->save($path, $_FILES['filename']['name']); 

       } catch (Exception $e) { 

       } 
      // echo $path;exit; 
       //this way the name is saved in DB 

      $data['image_path'] = $path.$_FILES['filename']['name']; 
     } 

。希望它能幫助你