How To? Create file/image/video upload in Backend for own module
This is an old revision of the document!
First lets suppose you created a custom module with the module creator.
Then in /app/code/local/Company/ModName/Block/Adminhtml/ModName/controllers/Adminhtml/ModuleNameController.php
Add ‘enctype’ ⇒ ‘multipart/form-data’ That should help to get something in $_FILES
You should have something looking like this:
- $form = new Varien_Data_Form(array(
- 'id' => 'edit_form',
- 'action' => $this->getUrl('*/*/save', array('id' => $this->getRequest()->getParam('id'))),
- 'method' => 'post',
- 'enctype' => 'multipart/form-data'
- )
- );
Then in /app/code/local/Company/ModName/Block/Adminhtml/ModName/Edit/Tab/Form.php
in _prepareForm Add your image field (you can add a field of type file or imagefile for vidéos !
- $fieldset->addField('fileinputname', 'image', array(
- 'label' => Mage::helper('pictos')->__('File label'),
- 'required' => false,
- 'name' => 'fileinputname',
- ));
Then in /app/code/local/Company/ModName/Block/Adminhtml/ModName/controllers/Adminhtml/ModuleNameController.php again after if ($data = $this→getRequest()→getPost()) { in saveAction()
- $uploader = new Varien_File_Uploader('fileinputname');
- $uploader->setAllowedExtensions(array('jpg','jpeg','gif','png')); // 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 ;
- $uploader->save($path);
- $data['fileinputname'] = $_FILES['fileinputname']['name'];
That’s it you’re done.

