0

Varien_File_Uploader I need to know how to upload multiple files, received with different name.

ex:

1-->

<input name="userfile" type="file"> 

2-->

<input name="coverimage" type="file">

my current code is able to receive only one file.

if (isset($_FILES['userfile']['name']) && $_FILES['userfile']['name'] != '') {
 try {
 $uploader = new Varien_File_Uploader('userfile');
 echo 'testing';
 $uploader->setAllowedExtensions(array('png', 'jpg','gif'));
 $uploader->setAllowRenameFiles(true);
 $uploader->setFilesDispersion(false);
 $currentTimestamp = Mage::getModel('core/date')->timestamp(time()); 
 $folder_name_date = date('Y_m_d', $currentTimestamp);
 $path = Mage::getBaseDir('media') . DS . 'uploads' . DS . $folder_name_date;
 if(!is_dir($path)){
 mkdir($path, 0777, true);
 }
 $uploader->save($path, $_FILES['userfile']['name'] ); 
 $newFilename = $uploader->getUploadedFileName();
 echo $path. DS .$newFilename;
 } catch (Exception $e) {
 echo "Error";
 }
 } else {
 echo 'File is not set';
 }
asked Sep 25, 2015 at 2:11
4
  • use foreach loop. Commented Sep 25, 2015 at 2:14
  • :D any other way. Varien_File_Uploader inbuild method? Commented Sep 25, 2015 at 2:19
  • why do you want to make things complex. just go on with foreach loop. That is the solution for this. There is no "Magento Way" of doing this. So go on with "PHP Way" Commented Sep 25, 2015 at 2:21
  • otherwise you have to repeat your whole code for other file type :), do you want to add some thing more in your query i am eager know that? Commented Sep 25, 2015 at 2:22

1 Answer 1

1

Magento uses Varien_File_Uploader to upload files within the system. When you want to upload multiple files, then use above class in a foreach loop. As far as I know, there is no "Magento way" for uploading multiple files. So go on with "PHP Way". ie use a foreach loop. This is an example

$_files = array(
 'userfile' => array(
 'allowed_extensions' => array('png', 'jpg', 'gif'),
 'rename_file' => true,
 'file_dispersion' => true,
 'file_path' => 'path/to/the/file'
 ),
 'coverimage' => array(
 'allowed_extensions' => array('png', 'jpg', 'gif'),
 'rename_file' => true,
 'file_dispersion' => true,
 'file_path' => 'path/to/the/file'
 )
);
foreach ($_files as $filename => $fileOpt) {
 if (isset($_FILES[$filename]['name']) && $_FILES[$filename]['name'] != '') {
 try {
 $uploader = new Varien_File_Uploader($filename);
 $uploader->setAllowedExtensions(array($fileOpt['allowed_extensions']));
 $uploader->setAllowRenameFiles($fileOpt['rename_file']);
 $uploader->setFilesDispersion($fileOpt['file_dispersion']);
 //$currentTimestamp = Mage::getModel('core/date')->timestamp(time()); 
 //$folder_name_date = date('Y_m_d', $currentTimestamp);
 //$path = Mage::getBaseDir('media') . DS . 'uploads' . DS . $folder_name_date;
 if(!is_dir($fileOpt['file_path'])){
 mkdir($path, 0777, true);
 }
 $uploader->save($fileOpt['file_path'], $_FILES[$filename]['name'] ); 
 $newFilename = $uploader->getUploadedFileName();
 echo $fileOpt['file_path']. DS .$newFilename;
 } catch (Exception $e) {
 echo "Error";
 }
 } else {
 echo 'File is not set';
 }
}
answered Sep 25, 2015 at 2:33

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.