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

Is covid virus really airborne?

  If airborne transmission is a significant factor in the pandemic, especially in crowded spaces with poor ventilation, the consequences for containment will be significant. How did the experts reach this conclusion? Reviewing existing research, the six experts from the UK, US and Canada identified 10 streams of evidence that collectively support the hypothesis that SARS-CoV-2 primarily transmits through the airborne route. 1. Super-spreading events account for substantial SARS-CoV-2 transmission. Indeed, the authors wrote, such events may be the  pandemic ’s primary drivers. Detailed analyses of human behaviours and other variables in concerts, cruise ships etc have shown patterns “consistent with airborne spread of SARS-CoV-2 that cannot be adequately explained by droplets or fomites”, they wrote. 2. Long-range transmission of SARS-CoV-2 between people in adjacent rooms has been documented in quarantine hotels, but never in each other’s presence. 3. Asymptomatic or pre-sympt...

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...