How To? Create file/image/video upload in Backend for own module

Last modified by somesid on Fri, June 25, 2010 09:35
Source|Old Revisions  

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:

  1. $form = new Varien_Data_Form(array(
  2.         'id' => 'edit_form',
  3.         'action' => $this->getUrl('*/*/save', array('id' => $this->getRequest()->getParam('id'))),
  4.         'method' => 'post',
  5.         'enctype' => 'multipart/form-data'
  6.    )
  7.  );

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 !

  1. $fieldset->addField('fileinputname', 'image', array(
  2.           'label'     => Mage::helper('pictos')->__('File label'),
  3.           'required'  => false,
  4.           'name'      => 'fileinputname',
  5. ));

Then in /app/code/local/Company/ModName/Block/Adminhtml/ModName/controllers/Adminhtml/ModuleNameController.php again after if ($data = $this→getRequest()→getPost()) { in saveAction()

  1. $uploader = new Varien_File_Uploader('fileinputname');
  2. $uploader->setAllowedExtensions(array('jpg','jpeg','gif','png')); // or pdf or anything
  3.  
  4.  
  5. $uploader->setAllowRenameFiles(false);
  6.  
  7. // setAllowRenameFiles(true) -> move your file in a folder the magento way
  8. // setAllowRenameFiles(true) -> move your file directly in the $path folder
  9. $uploader->setFilesDispersion(false);
  10.    
  11. $path = Mage::getBaseDir('media') . DS ;
  12.                
  13. $uploader->save($path);
  14.  
  15. $data['fileinputname'] = $_FILES['fileinputname']['name'];

That’s it you’re done.




 

Magento 2 GitHub Repository

Magento Job Board - Some sort of tag line goes here

Latest Posts| View all Jobs