Magento - 无法在自定义模块中上传文件

时间:2015-09-14 21:05:59

标签: php image file magento upload

我正在编辑magento中的自定义插件,我需要在其中添加一个字段来上传图像,并将其与其他数据一起保存在数据库中。该表单可以与其他数据以及数据库记录一起使用。但是,似乎$ _FILES变量始终返回空。更奇怪的是,每当我尝试在模块中上传图像时,始终在媒体目录中生成一个名为“cache_2ca019d1e2db75b611e5f3aa5c932970”的文件。

我发现这里有类似问题的人,但所提出的解决方案都不适合我。我输了= /

这是我的表单文件

int

这是我的控制器文件

<?php 
class SmashingMagazine_BrandDirectory_Block_Adminhtml_Brand_Edit_Form
extends Mage_Adminhtml_Block_Widget_Form
{
protected function _prepareForm()
{
    // instantiate a new form to display our brand for editing
    $form = new Varien_Data_Form(array(
        'id' => 'edit_form',
        'action' => $this->getUrl(
            'smashingmagazine_branddirectory_admin/brand/edit', 
            array(
                '_current' => true,
                'continue' => 0,
            )
        ),
        'method' => 'post',
        'enctype' => 'multipart/form-data'
    ));
    $form->setUseContainer(true);
    $this->setForm($form);

    // define a new fieldset, we only need one for our simple entity
    $fieldset = $form->addFieldset(
        'general',
        array(
            'legend' => $this->__('Brand Details')
        )
    );

    $brandSingleton = Mage::getSingleton(
        'smashingmagazine_branddirectory/brand'
    );

    // add the fields we want to be editable
    $this->_addFieldsToFieldset($fieldset, array(
        'name' => array(
            'label' => $this->__('Name'),
            'input' => 'text',
            'required' => true,
        ),
        'url_key' => array(
            'label' => $this->__('URL Key'),
            'input' => 'text',
            'required' => true,
        ),
        'image' => array(
            'label' => $this->__('Image'),
            'input' => 'image',
            'required' => true,
            'disabled' => false,
            'readonly' => true, 
        ),
        'visibility' => array(
            'label' => $this->__('Visibility'),
            'input' => 'select',
            'required' => true,
            'options' => $brandSingleton->getAvailableVisibilies(),
        ),


        /**
         * Note: we have not included created_at or updated_at,
         * we will handle those fields ourself in the Model before save.
         */
    ));

    return $this;
}

/**
 * This method makes life a little easier for us by pre-populating 
 * fields with $_POST data where applicable and wraps our post data in 
 * 'brandData' so we can easily separate all relevant information in
 * the controller. You can of course omit this method entirely and call
 * the $fieldset->addField() method directly.
 */
protected function _addFieldsToFieldset(
    Varien_Data_Form_Element_Fieldset $fieldset, $fields)
{
    $requestData = new Varien_Object($this->getRequest()
        ->getPost('brandData'));

    foreach ($fields as $name => $_data) {
        if ($requestValue = $requestData->getData($name)) {
            $_data['value'] = $requestValue;
        }

        // wrap all fields with brandData group
        $_data['name'] = "brandData[$name]";

        // generally label and title always the same
        $_data['title'] = $_data['label'];

        // if no new value exists, use existing brand data
        if (!array_key_exists('value', $_data)) {
            $_data['value'] = $this->_getBrand()->getData($name);
        }

        // finally call vanilla functionality to add field
        $fieldset->addField($name, $_data['input'], $_data);
    }

    return $this;
}

/**
 * Retrieve the existing brand for pre-populating the form fields.
 * For a new brand entry this will return an empty Brand object.
 */
protected function _getBrand() 
{
    if (!$this->hasData('brand')) {
        // this will have been set in the controller
        $brand = Mage::registry('current_brand');

        // just in case the controller does not register the brand
        if (!$brand instanceof 
                SmashingMagazine_BrandDirectory_Model_Brand) {
            $brand = Mage::getModel(
                'smashingmagazine_branddirectory/brand'
            );
        }

        $this->setData('brand', $brand);
    }

    return $this->getData('brand');
}
}

提前致谢。

1 个答案:

答案 0 :(得分:1)

// add the fields we want to be editable
    $this->_addFieldsToFieldset($fieldset, array(
        'name' => array(
            'label' => $this->__('Name'),
            'input' => 'text',
            'required' => true,
        ),
        'url_key' => array(
            'label' => $this->__('URL Key'),
            'input' => 'text',
            'required' => true,
        ),
        'image' => array(
            'label' => $this->__('Image'),
            'input' => 'image',
            'name' => 'image',
            'required' => true,
            'disabled' => false, 
        ),
        'visibility' => array(
            'label' => $this->__('Visibility'),
            'input' => 'select',
            'required' => true,
            'options' => $brandSingleton->getAvailableVisibilies(),
        ),


        /**
         * Note: we have not included created_at or updated_at,
         * we will handle those fields ourself in the Model before save.
         */
    ));

通过在图像上添加名称属性来尝试此操作。