Varien_File_Uploader没有在magento中上传自定义模块中的文件

时间:2014-02-27 09:53:36

标签: 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();
}
}
?>

5 个答案:

答案 0 :(得分: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]
            ));

答案 1 :(得分:0)

Waseem,请尝试上传图片代码..

$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);

答案 2 :(得分: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'返回。

如果您要上传单个文件,请设置{{1}}或查看此question

答案 3 :(得分:0)

尝试使用

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

而不是您目前使用的。

答案 4 :(得分: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);