How to Upload Multiple Files with Varien_File_Uploader in Magento 1.8

magento-1.8multiple-file-uploadvarien-file-uploader

This question is related to my previous question where I'd created a tab form in admin using a template file. The content of template file is:

<div class="entry-edit">
    <div class="entry-edit-head">
        <h4 class="icon-head head-edit-form fieldset-legend">Images</h4>
    </div>
    <div class="fieldset">
        <div class="hor-scroll">
            <table class="form-list container">
                <tr class="wrapper-tr">
                    <td class="value">
                        <input type="file" name="images[]"/>
                    </td>
                    <td class="label">
                        <span class="remove">Remove</span>
                    </td>
                </tr>
            </table>
            <input type="button" class="add" value="Add Image"/>
        </div>
    </div>
</div>

<script>
    jQuery(document).ready(function() {
        jQuery('.add').click(function() {
            var wrapper = "<tr class='wrapper-tr'>" +
                    "<td class='value'><input type='file' name='images[]'></td>" +
                    "<td class='label'><span class='remove'>Remove</span></td>" +
                    "</tr>";
            jQuery(wrapper).find('.remove').on('click', function() {
                jQuery(this).parent('.wrapper-tr').remove();
            });
            jQuery(wrapper).appendTo('.container');
        });
        jQuery('.container').on('click', 'span.remove', function() {
            if (jQuery('.wrapper-tr').length > 1) {
                jQuery(this).parents('.wrapper-tr').remove();
            } else {
                alert('at least one image need to be selected');
            }
        });
    });
</script>

for uploading multiple files.

But as my input type name is images[] that's why in my controller's saveAction() I'm unable to upload file using Varien_File_Uploader as:

$uploader = new Varien_File_Uploader('images');

what value should I pass in Varien_File_Uploader constructor in order to be able to upload file?

Update
I tried logging and found this warning:

Warning: file_exists() expects parameter 1 to be a valid path, array given in /var/www/mageqb/lib/Varien/File/Uploader.php on line 150

Code in my controller is:

foreach ($_FILES['images']['name'] as $key => $image) {
    Mage::log('looping');
    if (empty($image)) {
        Mage::log('continue');
        continue;
    }
    try {
        Mage::log('uploading');
        $uploader = new Varien_File_Uploader('images');

        // Any extention would work
        $uploader->setAllowedExtensions(array('jpg', 'jpeg', 'gif', 'png'));
        $uploader->setAllowRenameFiles(true);

        $uploader->setFilesDispersion(false);

        $path = Mage::getBaseDir('media') . DS . 'authors' . DS;
        $img = $uploader->save($path, $_FILES['images']['name'][$key]);
        Mage::log($img['file']);
    } catch (Exception $e) {
        echo $e->getMessage();
        Mage::log($e->getMessage());
    }
}

Best Answer

this is the expected code

$uploader = new Varien_File_Uploader(
        array(
    'name' => $_FILES['images']['name'][$key],
    'type' => $_FILES['images']['type'][$key],
    'tmp_name' => $_FILES['images']['tmp_name'][$key],
    'error' => $_FILES['images']['error'][$key],
    'size' => $_FILES['images']['size'][$key]
        )
);
Related Topic