6

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());
    }
}
18

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]
        )
);
  • What is the $key in this? – Akhil Gupta Jun 24 '16 at 12:19
  • I tried above code but only one file is uploaded.can you please help – Jenith Samuel Mar 1 '17 at 14:28
  • 1
    @J.Jenith: perhaps, you have got only one item in $_FILES. can you verify the size of array received at your end? – Mohammad Faisal Mar 1 '17 at 15:01
  • Thanks , yes i got one item but i upload multiple files jQuery('#front_files input[type="file"]').each(function(index){ var file_id = jQuery(this).attr("id"); file_data = jQuery(this)[0].files; // get multiple files from input file if(file_data.length >0 ){ formdata.append('upload_artworks', file_data[0]); // we can put more than 1 image file } }); – Jenith Samuel Mar 2 '17 at 7:30
  • 1
    @J.Jenith: so this is clearly a JavaScript issue where in your data:{collection: files} you have just one item. Its better you should ask a new question for the same. – Mohammad Faisal Mar 2 '17 at 9:53

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.