【问题标题】:Uploading multiple files userfile[] array?上传多个文件 userfile[] 数组?
【发布时间】:2012-04-14 04:20:52
【问题描述】:

我正在尝试上传多张图片,并且我设置了一个 jquery 插件,因此我可以浏览多个文件并选择它们。

我遇到的唯一问题是访问控制器中的文件数组,有人知道我该怎么做吗?谢谢。

查看:

<input type="file" name="userfile[]" id="userfile" class="multi" />

【问题讨论】:

    标签: arrays codeigniter upload


    【解决方案1】:

    去年我在一个项目中遇到了同样的问题,经过一番搜索,我找到了一个完美的功能。

    我不相信它,但不记得我在哪里找到它,所以如果有人知道,请链接回作者。

    确保表单具有这样命名的文件

    <input type="file" name="userfile"/>
    <input type="file" name="userfile2" />
    <input type="file" name="userfile3" /> ..etc
    

    或者

     <input type="file" name="userfile[]" />
    

    函数..

    function multiple_upload($upload_dir = 'uploads', $config = array())
    {
        $files = array();
    
        if(empty($config))
        {
            $config['upload_path'] = '../path/to/file';
            $config['allowed_types'] = 'gif|jpg|jpeg|jpe|png';
            $config['max_size']      = '800000000';
        }
    
            $this->load->library('upload', $config);
    
            $errors = FALSE;
    
            foreach($_FILES as $key => $value)
            {            
                if( ! empty($value['name']))
                {
                    if( ! $this->upload->do_upload($key))
                    {                                           
                        $data['upload_message'] = $this->upload->display_errors(ERR_OPEN, ERR_CLOSE); // ERR_OPEN and ERR_CLOSE are error delimiters defined in a config file
                        $this->load->vars($data);
    
                        $errors = TRUE;
                    }
                    else
                    {
                        // Build a file array from all uploaded files
                        $files[] = $this->upload->data();
                    }
                }
            }
    
            // There was errors, we have to delete the uploaded files
            if($errors)
            {                    
                foreach($files as $key => $file)
                {
                    @unlink($file['full_path']);    
                }                    
            }
            elseif(empty($files) AND empty($data['upload_message']))
            {
                $this->lang->load('upload');
                $data['upload_message'] = ERR_OPEN.$this->lang->line('upload_no_file_selected').ERR_CLOSE;
                $this->load->vars($data);
            }
            else
            {
                return $files;
            }
        } 
    

    现在我已经有一段时间没有使用它了,所以如果您需要任何额外的设置帮助,请告诉我。

    【讨论】:

    • 嘿,谢谢,我实际上已经弄明白了……万岁!
    • 我讨厌 Codeigniter 不支持像 &lt;input type="file" name="userfile[]"/&gt; 这样的多个文件输入。这个“解决方案”是一个实际的解决方法。但我必须证明这个解决方案是最好的之一。
    • 您好,我想请教一下如何在我的 html 表单也驻留的 1 页上进行设置。所以我自己提交我的表格。我应该传递什么参数。谢谢
    【解决方案2】:

    要使用jquery多次上传使用html元素数组[]在接收数据的行为中开发如下sobrescevi全局变量$_FILES PHP以便函数do_upload可以稍后读取。见:

                #copy the original array;               
                $temp_array;
                foreach($_FILES['fil_arquivo'] as $key => $val)
                {
                    $i = 0;
                    foreach($val as $new_key)
                    {
                        $temp_array[$i][$key] = $new_key;
                        $i++;
                    }
                    //
                }
    
                $i = 0;
                foreach($temp_array as $key => $val)
                {
                    $_FILES['file'.$i] = $val;
                    $i++;
                }
    
                #clear the original array;
                unset($_FILES['fil_arquivo']);
    
                $upload_path = 'media/arquivos';
    
                $config['upload_path'] = $upload_path;
                $config['allowed_types'] = 'doc|docx|xls|xlsx|ppt|pptx|pdf|txt|jpg|png|jpeg|bmp|gif|avi|flv|mpg|wmv|mp3|wma|wav|zip|rar';
                $config['encrypt_name'] = true;
    
                $this->upload->initialize($config);
                foreach($_FILES as $key => $value)
                {            
                    if( ! empty($value['name']))
                    {
                        if($this->upload->do_upload($key))
                        {                                           
                            $this->upload->data();
                         }
                    }
                }
    

    【讨论】:

    • 错了,他们不只将文件名发送给do_upload()方法,它的整个数组
    【解决方案3】:

    上面ojjwood提供的HTML会产生多个输入框。如果您只想有一个输入框但仍要上传多个文件,则需要在您的&lt;input&gt; 元素中包含属性multiple="multiple"name="userfile[]" 中的 [] 允许发布的数据是一个数组,而不仅仅是一个值。

    在 Codeigniter/PHP 方面,您需要使用 $_POST['userfile] 而不是 $this-&gt;input-&gt;post('userfile') 来处理数组。

    【讨论】:

    • 你能说出它实际上是如何工作的吗? $_FILES 中的所有内容也是一个数组
    猜你喜欢
    • 2015-12-30
    • 2012-05-31
    • 2019-06-22
    • 2019-05-22
    • 2021-02-26
    • 2012-07-16
    • 2017-04-18
    • 2019-05-25
    相关资源
    最近更新 更多