Magento upload multiple image files


Magento upload multiple image files : You can upload multiple images in magento as below.

Magento upload multiple image files

Suppose you have following image file upload option in your magento form.

<input  name="images[]" type="file"> Image 1
<input  name="images[]" type="file"> Image 2
<input  name="images[]" type="file"> Image 3
<input  name="images[]" type="file"> Image 4
<input  name="images[]" type="file"> Image 5

Now add the following code in your form Action ie. In your Controller method :

function uploadAdvertisementImage(){
foreach($_FILES['images']['name']  as $key =>$image){
		if (!empty($image)) {
			try {
				//$uploader = new Varien_File_Uploader('image_url');
                                $uploader = new Varien_File_Uploader(array(
                                    'name' => $_FILES['image_url']['name'][$key],
                                    'type' => $_FILES['image_url']['type'][$key],
                                    'tmp_name' => $_FILES['image_url']['tmp_name'][$key],
                                    'error' => $_FILES['image_url']['error'][$key],
                                    'size' => $_FILES['image_url']['size'][$key]
                                     ));

				$uploader->setAllowedExtensions(array('jpg', 'jpeg', 'png'));
				$uploader->setAllowRenameFiles(false);
				$uploader->setFilesDispersion(false);
				$path = Mage::getBaseDir('media') . DS . 'advertisement';
				$fileName = time()."_".substr( base_convert( time(), 10, 30 ).md5( microtime() ), 0, 10 ).'.'.$uploader->getFileExtension();
				
				$uploader->save($path, $fileName);                              
                                $rand = rand(0,20000);
                                $pathUrl = Mage::getUrl('media/images/');
                                $fileNames[$key] = trim($fileName);
			} catch (Exception $e) { 
                         Mage::log('Error in upload');                        
                        
                         
                        }
		}
            } 
}

User Ans/Comments

📖 Read More


Advertisements