Magento图像字段管理网格不保存

时间:2013-04-23 09:23:22

标签: image magento upload module save

我在这里遇到问题,我认为这基本上只是因为我错过了一步。当我保存文件时,图像会保存到指定的目录。但是,当我去编辑表格时,我上传的图像不会显示。

这是我建立的第一个网格模块,所以我对它的工作原理有点不清楚。

在Form.php中,我的图片字段如下:

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

我已将enctype设置为:'enctype'=> '多部分/格式数据'

我的控制器在if($ postData = $ this-> getRequest() - > 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以显示?

我希望这是足够的信息,并提前感谢。

2 个答案:

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

。希望它会帮助你

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