编程语言
首页 > 编程语言> > php – 使用Varien_File_Uploader上传多个文件

php – 使用Varien_File_Uploader上传多个文件

作者:互联网

这个问题与我以前的question有关,我在admin中使用模板文件创建了一个选项卡表单.模板文件的内容是:

<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>

用于上传多个文件.

但是因为我的输入类型名称是images [],这就是为什么在我的控制器的saveAction()中我无法使用Varien_File_Uploader上传文件:

$uploader = new Varien_File_Uploader('images');

我应该在Varien_File_Uploader构造函数中传递什么值才能上传文件?

更新
我尝试登录并发现此警告:

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

我控制器中的代码是:

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

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

        // Set the file upload mode
        // false -> get the file directly in the specified folder
        // true -> get the file in the product like folders
        //  (file.jpg will go in something like /media/f/i/file.jpg)
        $uploader->setFilesDispersion(false);

        // We set media as the upload dir
        $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());
    }
}

解决方法:

这是预期的代码

$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]
        )
);

标签:php,file-upload,magento,multiple-files
来源: https://codeday.me/bug/20190715/1467318.html