Skip to main content

Add multiple file/images in magento custom module

For upload multiple files, first we have to change input type in Abstract.php
 go to lib/Varien/Data/Form/Element/Abstract.php

look for function and replace

 public function getElementHtml()
    {
if($this->getType()=='file' && $this->getMultiple())
        $_multiple = ' multiple';
$html = '<input id="'.$this->getHtmlId().'" name="'.$this->getName()
         .'" value="'.$this->getEscapedValue().'" '.$this->serialize($this->getHtmlAttributes()).$_multiple.'/>'."\n";
$html.= $this->getAfterElementHtml();
return $html;
    }

now open form.php in your module say:

app/code/community/module name/namespace/Block/Adminhtml/Libraryfile/Edit/Tab/Form.php

add field:
$fieldset->addField('file_path', 'file', array(
'label' => Mage::helper('libraryfile')->__('File'),
'name' => 'file_path[]',
'multiple' => 'multiple',
'mulitple' => true,
'note' => '(*.pdf, *.txt, *.doc, *.docx, *.xls, *.xlsx)',

));

now change in  your controller :

add this into your saveAction method:

$i=0; 
 foreach ($_FILES['file_path']['name'] as $key => $image) { 
 $path = Mage::getBaseDir('media') . DS . 'libraryfile' . DS .'libraryfile'.DS;

 $uploader = new Mage_Core_Model_File_Uploader( 
 array( 'name' => $_FILES['file_path']['name'][$i],
 'type' => $_FILES['file_path']['type'][$i], 
 'tmp_name' => $_FILES['file_path']['tmp_name'][$i], 
 'error' => $_FILES['file_path']['error'][$i],
 'size' => $_FILES['file_path']['size'][$i] )); 
 $uploader->setAllowedExtensions(array('pdf','txt','doc','docx','xlsx','xls'));
 $uploader->setAllowRenameFiles(false); 
 $uploader->setFilesDispersion(false);
 $destFile = $path.$image; 

 $filename = $uploader->save($path, $image);
 $uploader->save($path, $filename);
 $post_data['file_path']='/media/libraryfile/libraryfile/'.$image;
 //addedby Ankur Rai 
 $i++ }

Thats it :) 

Enjoy

Comments

Popular posts from this blog

Parsing Domain Name From URL In PHP

To get Domain name from the url, we can use parse_url() php function. This would filter the domain name from the given url. $domain = str_ireplace ( 'www.' , '' , parse_url ( $url , PHP_URL_HOST )); This would return the google.com for both http://google.com and http://www.google.com

How to display Image in grid

Write this code in Grid.php:- $this->addColumn("data", array( "header" => Mage::helper("userdesign")->__("Design"), "index" => "image", 'align'     =>'center', 'renderer'  => 'userdesign/Adminhtml_Userdesign_Renderer_Image' )); Then make a "Renderer" folder in module( location - Grid.php) In Renderer Make a PHP file , Named Image.php and put this code <?php class Userdesign_Userdesign_Block_Adminhtml_Userdesign_Renderer_Image extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract{           public function render(Varien_Object $row)     { $designUrl=Mage::getBaseUrl('web').'design/designs/';         $html = '<img width="75" height="75" ';          $value = $row->getData('designId');       $html.= 'src="' . Mage::getBaseUrl('web')....