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';
    }
  • use foreach loop. – Rajeev K Tomy Sep 25 '15 at 2:14
  • :D any other way. Varien_File_Uploader inbuild method? – Dushan Sep 25 '15 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" – Rajeev K Tomy Sep 25 '15 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? – amit_game Sep 25 '15 at 2:22
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';
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.