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

get directory paths, get Base URLs, get URLs in static block, get secure URL in Magento

1) Get Directory paths  Mage::getBaseDir()  //output : /var/www/html/magento Mage::getBaseDir('app')  //output : /var/www/html/magento/app Mage::getBaseDir('media') //output : /var/www/html/magento/media Mage::getBaseDir(‘design’) => Get design directory path Mage::getBaseDir(‘code’) => Gives code directory file path Mage::getBaseDir(‘lib’) => Gives lib directory file path Mage::getBaseDir(‘skin’) => Gives skin directory file path Mage::getBaseDir(‘var’) => Gives var directory file path Mage::getBaseDir(‘cache’) => Gives cache directory file path Mage::getBaseDir(‘log’) => Gives log directory file path       2) Get Base URL  Mage::getBaseUrl() => Get base url path e.g. http://yourwebsite.com/  Mage::getBaseUrl('media') => Get MEDIA folder path e.g. http://yourwebsite.com/media/  Mage::getBaseUrl('js') => Get JS folder path e.g. http://yourwebsite.com/js/  Mage::getBaseUrl('skin') => Get...