2011-11-09 58 views
1

我需要爲Magento博客模塊(AW博客)中的帖子創建圖片上傳字段。 因此,每個帖子都需要包含一張圖片。Magento自定義圖片上傳不起作用

我編輯了以下文件: /home/clients/websites/w_gale/public_html/gale/app/code/community/AW/Blog/Block/Manage/Blog/Edit/Tab/Form.php

加入字段集是這樣的:

$fieldset->addField('fileinputname', 'image', array(
      'label'  => Mage::helper('blog')->__('Upload image'), 
      'required' => false, 
      'name'  => 'fileinputname', 
      'required' => true, 
      'style'  => 'height:100px;border:2px solid #999;', 
    )); 

在這個文件的頂部,在正確的位置,我所定義的形式是這樣的:

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

在Blogcontroller.php添加以下代碼,只是b ellow if ($data = $this->getRequest()->getPost()) { line

if(isset($_FILES['fileinputname']['name']) && (file_exists($_FILES['fileinputname']['tmp_name']))) { 
      try { 

       $uploader = new Varien_File_Uploader('fileinputname'); 

       $uploader->setAllowedExtensions(array('jpg','jpeg','gif','png')); // or pdf or anything 

       $uploader->setAllowRenameFiles(true); 
       $uploader->setFilesDispersion(false); 

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

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

       $data['imageurl'] = $_FILES['fileinputname']['name']; 

      } catch(Exception $e) { 

      } 
     } else {  

      if(isset($data['fileinputname']['delete']) && $data['fileinputname']['delete'] == 1) 
       $data['imageurl'] = ''; 
      else 
       unset($data['fileinputname']); 
     } 

但是,上傳不起作用。我究竟做錯了什麼? 我在數據庫的相應字段中添加了一個特殊行。 當我手動輸入時,前端部分顯示數據庫值。

謝謝

回答

2

這個代碼從上傳圖像的數據幫助程序。你需要實現方法getBaseDir()(它返回你希望存儲你上傳的文件的目錄)。

/** 
* Upload image and return uploaded image file name or false 
* 
* @throws Mage_Core_Exception 
* @param string $scope the request key for file 
* @return bool|string 
*/ 
public function uploadImage($scope) 
{ 
    $adapter = new Zend_File_Transfer_Adapter_Http(); 
    $adapter->addValidator('ImageSize', true, $this->_imageSize); 
    $adapter->addValidator('Size', true, $this->_maxFileSize); 
    if ($adapter->isUploaded($scope)) { 
     // validate image 
     if (!$adapter->isValid($scope)) { 
      Mage::throwException(Mage::helper('mycompany_mymodule')->__('Uploaded image is not valid')); 
     } 
     $upload = new Varien_File_Uploader($scope); 
     $upload->setAllowCreateFolders(true); 
     $upload->setAllowedExtensions(array('jpg', 'gif', 'png')); 
     $upload->setAllowRenameFiles(true); 
     $upload->setFilesDispersion(false); 
     if ($upload->save($this->getBaseDir())) { 
      return $upload->getUploadedFileName(); 
     } 
    } 
    return false; 
} 
0

您使用的是正確的代碼。我通過使用正確的MYSQL數據類型解決了這個問題。當我改變了從「文」的數據類型爲「VARCHAR(255)」它解決了這個問題

而且......確保你添加以下代碼:

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

在/ app /碼/community/AW/Blog/Block/Manage/Blog/Edit/Form.php NOT:app/code/community/AW/Blog/Block/Manage/Blog/Edit/Tab/Form.php