2014-02-27 98 views
1

我試圖將圖像保存在我的模塊中,但圖像未從表單中保存。Varien_File_Uploader未上傳magento中的自定義模塊中的文件

$uploader = new Varien_File_Uploader('image');此代碼不工作我不知道爲什麼。循環在這一行中斷,並且控制從這裏退出循環。我如何保存圖像。

這是我保存功能控制器

public function saveAction() 
{ 
if ($this->getRequest()->getPost()) 
{ 
    try 
    { 
    $postData = $this->getRequest()->getPost(); 
    //echo "<pre>";print_r($postData); exit; 
    $articleModel = Mage::getModel('blog/article'); 
    $imgFilename = NULL; 
    if($_FILES['image']['name'] != '') 
    {//echo "<pre>"; echo count($_FILES['image']['name']); 
     foreach($_FILES['image']['name'] as $_FILES['image']['name']) 
     { 
     //print_r($_FILES['image']['name']); 
     try 
     { echo "1"; 
      $uploader = new Varien_File_Uploader('image'); echo "hi"; 
      //print_r($uploader);exit; 
      $uploader->setAllowedExtensions(array('jpg','jpeg','gif','png','flv')); 
      $uploader->setAllowRenameFiles(false); 
      $uploader->setFilesDispersion(false); 
      $uploader->setAllowCreateFolders(true); 
      // Set media as the upload dir 
      $media_path = Mage::getBaseDir('media') . DS . 'blog' . DS; 
      $imgFilename = time() . $postData['image']; 
      // Upload the image 
      //$uploader->save($media_path, $_FILES['image']['name']);echo "4"; 
      $uploader->save($media_path, $imgFilename); 
     } 
     catch (Exception $e) 
     { 
      Mage::log($e); 
      $this->_redirectError(502); 
     } 
      $data['image'] = $imgFilename; 
     } 
    } 
    else 
    {   
     if(isset($data['image']['delete']) && $data['image']['delete'] == 1) 
     $data['image'] = ''; 
     else 
     unset($data['image']); 
    } 
     //echo "out"; exit; 
    if($this->getRequest()->getParam('id') <= 0) 
     $articleModel->setCreatedTime(
     Mage::getSingleton('core/date') 
     ->gmtDate()); 
     $articleModel 
     ->addData($postData) 
     ->setUpdatedTime(
     Mage::getSingleton('core/date') 
     ->gmtDate()) 
     ->setId($this->getRequest()->getParam('id')) 
     ->save(); 
     $lastid = $articleModel->getId(); 
     if($data['image'] != '') 
     { 
     foreach($data['image'] as $img) 
     { 
      $imageModel=Mage::getModel('blog/image'); 
      $imageModel->setArticleId($lastid)->setImage($data['image'])->save(); 
     } 
     } 
     Mage::getSingleton('adminhtml/session') 
     ->addSuccess('successfully saved'); 
     Mage::getSingleton('adminhtml/session') 
     ->setarticleData(false); 
     $this->_redirect('*/*/'); 
     //return; 
     if ($this->getRequest()->getParam('back')) 
     { 
     $this->_redirect('*/*/edit',array('id' => $articleModel->getId())); 
     return; 
     } 
    } 
    catch (Exception $e) 
    { 
      Mage::getSingleton('adminhtml/session') 
      ->addError($e->getMessage()); 
      Mage::getSingleton('adminhtml/session') 
      ->setarticleData($this->getRequest() 
      ->getPost()); 
      $this->_redirect('*/*/edit', 
       array('id' => $this->getRequest() 
       ->getParam('id'))); 
      return; 
     } 
    } 
     $this->_redirect('*/*/'); 
} 

,這裏是我的形象

<?php 
class Vertax_Blog_Block_Adminhtml_Article_Edit_Tab_Image extends Mage_Adminhtml_Block_Widget_Form 
{ 
protected function _prepareForm() 
{ 

    $form = new Varien_Data_Form(); 
    $this->setForm($form); 
    $fieldset = $form->addFieldset('image_form', 
            array('legend'=>'image')); 
//$fieldset->addType('image', Mage::getConfig()->getBlockClassName('blog/adminhtml_article_helper_image')); 
$fieldset->addType('image', 'Vertax_Blog_Block_Adminhtml_Article_Helper_Image'); 

$fieldset->addField('image', 'image', array(
     'label'  => 'Image', 
     'required' => false, 
     'name'  => 'image[]', 
    'multiple' => 'multiple', 
    'mulitple' => true, 
    )); 

if (Mage::getSingleton('adminhtml/session')->getBlogPostData()) { 
     $form->setValues(Mage::getSingleton('adminhtml/session')->getBlogPostData()); 
     Mage::getSingleton('adminhtml/session')->setBlogPostData(null); 
    } elseif (Mage::registry('article_data')) { 
     $form->setValues(Mage::registry('article_data')->getData()); 
    } 
    return parent::_prepareForm(); 
} 
} 
?> 
+0

Waseem.can請您使用形式使用多個表單數據>告訴? –

+0

是的我正在使用多個表單數據。單個文件上傳工作正常,但上傳多個文件時,我卡住了。 – Waseem

+0

可以請你給我....形式文件? –

回答

0

克瓦,請嘗試上傳圖片代碼的形式..

$uploader = new Mage_Core_Model_File_Uploader('image'); 
      $uploader->setAllowedExtensions(array('jpg', 'jpeg', 'gif', 'png')); 

      $uploader->setFilesDispersion(true); 
$media_path = Mage::getBaseDir('media') . DS . 'blog' . DS; 
      $imgFilename = time() . $postData['image']; 
      // Upload the image 
      //$uploader->save($media_path, $_FILES['image']['name']);echo "4"; 
      $uploader->save($media_path, $imgFilename); 
+0

感謝阿米特,但仍然沒有得到任何東西,循環再次打破這一行'$上傳=新Mage_Core_Model_File_Uploader('圖像');' – Waseem

+0

請使用閃存加載器它...作品... –

0

在添加字段集時代碼出現問題

$fieldset->addField('image', 'image', array(
     'label'  => 'Image', 
     'required' => false, 
     'name'  => 'image[]', 
    'multiple' => 'multiple', 
    'mulitple' => true, 
    )); 

此處您已將名稱設置爲image[],而這又將返回數組爲$_FILES['image][name][],$_FILES['image][tmp_name][]

如果您要上傳單個文件設置'name' = 'image'或看到這個question

0

嘗試使用

$uploader = new Varien_File_Uploader($_FILES['image']);

,而不是你目前使用的是什麼。

1
$uploader = new Mage_Core_Model_File_Uploader(
     array(
       'name' => $_FILES['galleryImage']['name'][$i], 
       'type' => $_FILES['galleryImage']['type'][$i], 
       'tmp_name' => $_FILES['galleryImage']['tmp_name'][$i], 
       'error' => $_FILES['galleryImage']['error'][$i], 
       'size' => $_FILES['galleryImage']['size'][$i] 
      )); 
+0

使用它它爲我工作: ) – devJsha

+0

這是什麼$ i? –

0

我不得不訪問Magento的1.9.3.x的file -Key:

// Inside for loop 
$file = array(
    'name' => $files['name'][$i]['file'], 
    'type' => $files['type'][$i]['file'], 
    'tmp_name' => $files['tmp_name'][$i]['file'], 
    'error' => $files['error'][$i]['file'], 
    'size' => $files['size'][$i]['file'], 
); 

$uploader = new Varien_File_Uploader($file); 
相關問題