0

I have used this extension HOW TO CREATE A CUSTOM FORM IN MAGENTO ADMIN to create a custom form in my backend and the custom form has only one file input.

when i submit the form, it return form key only. so please check my code if there is anything wrong. thanks

AdminformController.php

class Turnkeye_Adminform_Adminhtml_AdminformController extends Mage_Adminhtml_Controller_Action
{
    public function indexAction()
    {
        $this->_registryObject();
        $this->loadLayout();
        $this->_setActiveMenu('turnkeye/form');
        $this->_addBreadcrumb(Mage::helper('turnkeye_adminform')->__('Form'), Mage::helper('turnkeye_adminform')->__('Form'));
        $this->renderLayout();
    }

    public function saveAction()
    {

        if ($data = $this->getRequest()->getPost()) {
            try
            {
                if(isset($_FILES['general[fileinputname]']['name']) and (file_exists($_FILES['fileinputname']['tmp_name']))) {
                    try {
                        echo "im here"; exit();
                        $uploader = new Varien_File_Uploader('fileinputname');
                        $uploader->setAllowedExtensions(array('csv')); // or pdf or anything


                        $uploader->setAllowRenameFiles(false);

                        // setAllowRenameFiles(true) -> move your file in a folder the magento way
                        // setAllowRenameFiles(true) -> move your file directly in the $path folder
                        $uploader->setFilesDispersion(false);

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

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

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

                    } catch(Exception $e) {
                        print_r($e);
                        die;
                    }
                }
            } catch (Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
                $this->_redirect('*/adminform/index');
                return;
            }
        }
        Mage::getSingleton('adminhtml/session')->addError(Mage::helper('turnkeye_adminform')->__('Unable to find item to save'));
        $this->_redirect('*/adminform/index');
    }

    protected function _registryObject()
    {
//        Mage::register('turnkeye_adminform', Mage::getModel('turnkeye_adminform/form'));
    }
}

app\code\community\Turnkeye\Adminform\Block\Adminhtml\Form\Edit\Form.php

class Turnkeye_Adminform_Block_Adminhtml_Form_Edit_Form extends Mage_Adminhtml_Block_Widget_Form
{
    protected function _prepareForm()
    {
        $form = new Varien_Data_Form(array(
         'id' => 'edit_form',
        'action' => $this->getUrl('*/*/save'),
        'method' => 'post',
        'enctype' => 'multipart/form-data'
        ));

        $form->setUseContainer(true);
        $this->setForm($form);
        return parent::_prepareForm();
    }
}

app\code\community\Turnkeye\Adminform\Block\Adminhtml\Form\Edit\Tab\General.php

<?php

class Turnkeye_Adminform_Block_Adminhtml_Form_Edit_Tab_General extends Mage_Adminhtml_Block_Widget_Form
{
    protected function _prepareForm()
    {
        $helper = Mage::helper('turnkeye_adminform');
        $form = new Varien_Data_Form();
        $form->setHtmlIdPrefix('general_');
        $form->setFieldNameSuffix('general');

        $fieldset = $form->addFieldset('display', array(
            'legend'       => $helper->__('Display Settings'),
            'class'        => 'fieldset-wide'
        ));

        $fieldset->addField('fileinputname', 'file', array(
          'label'     => $helper->__('Aramex Sheet'),
          'required'  => false,
          'name'      => 'fileinputname',
        ));

        if (Mage::registry('turnkeye_adminform')) {
            $form->setValues(Mage::registry('turnkeye_adminform')->getData());
        }

        $this->setForm($form);
        return parent::_prepareForm();
    }
}

here its a picture of html source code Html source code

0

You have used wrong syntax in AdminformController.php for getting FILE data. Use below code to access different attributes for submitted file:

<?php
    echo "Name: ".$_FILES["general"]["name"]["fileinputname"];
    echo "Type: ".$_FILES["general"]["type"]["fileinputname"];
    echo "Temp Name: ".$_FILES["general"]["tmp_name"]["fileinputname"];
    echo "Size: ".$_FILES["general"]["size"]["fileinputname"];
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.