【问题标题】:uploading images in codeigniter, is userfile required?在codeigniter中上传图片,需要用户文件吗?
【发布时间】:2013-02-13 21:09:05
【问题描述】:

我正在使用以下功能将我的图像文件上传到我的服务器(本地主机)。很好,正在上传图片。但是我需要两个图像字段,因此一旦单击提交按钮,两个图像都应该上传。我使用了此处描述的功能 Codeigniter multiple file upload ,但它不起作用。

我收到此错误消息

A PHP Error was encountered

Severity: Warning

Message: is_uploaded_file() expects parameter 1 to be string, array given

Filename: libraries/Upload.php

Line Number: 161

嗯,我不明白错误在哪里。 我用来上传单张图片的功能是

function do_upload()
    {
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'jpeg|png|gif';
        $config['max_size'] = '0';
        $config['max_width']  = '0';
        $config['max_height']  = '0';

        $this->load->library('upload', $config);

        if ( ! $this->upload->do_upload())
        {
            $error = array('error' => $this->upload->display_errors());
           //failed display the errors
        }
        else
        {
            //success

        }
    }

此外,我还想问一下我可以更改我选择的输入字段名称吗?即我总是需要实现 <input type="file" name="userfile"/> 。是否可以更改名称?我尝试更改它并收到消息未选择文件,所以这一定意味着我无法更改它。

【问题讨论】:

    标签: php html codeigniter file-upload


    【解决方案1】:

    您必须像您提供的链接中显示的那样循环浏览上传的文件。

    function do_upload() {
            $config['upload_path'] = './uploads/';
            $config['allowed_types'] = 'jpeg|png|gif';
            $config['max_size'] = '0';
            $config['max_width']  = '0';
            $config['max_height']  = '0';
    
            $this->load->library('upload', $config);
    
            foreach ($_FILES as $key => $value) {
    
                if (!empty($value['tmp_name'])) {
    
                    if ( ! $this->upload->do_upload($key)) {
                        $error = array('error' => $this->upload->display_errors());
                        //failed display the errors
                    } else {
                        //success
                    }
    
                }
            }
    }
    

    并尝试像这样使用 HTML:

    <input type="file" name="file1" id="file_1" />
    <input type="file" name="file2" id="file_2" />
    <input type="file" name="file3" id="file_3" />
    

    您可以随意更改输入字段的名称。

    【讨论】:

    • 我仍然收到我在问题中发布的错误消息。
    • 只上传一张图片
    • 您是否已将//failed display the errors//success 替换为代码?如果是,那么那里有什么。
    • 是的..我正在使用重定向..我的错..对不起..它有效..谢谢:)
    • 我认为您不想在循环内调用 initialize(),除非您打算更改每个图像文件
    【解决方案2】:

    如果您使用的是文件多选,请执行以下操作:

    HTML(带有数组名的HTML5文件输入允许选择多个文件):

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

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

    CodeIgniter 中的 PHP:

    // load upload library (put your own settings there)
    $this->load->library('upload', array('allowed_types' => 'svg|gif|jpg|png', 'upload_path' => $GLOBALS['config']['upload_path'], ));
    
    // normalise files array
    $input_name = 'userfile'; // change it when needed to match your html
    $field_names = array('name', 'type', 'tmp_name', 'error', 'size', );
    $keys = array();
    foreach($field_names as $field_name){
        if (isset($_FILES[$input_name][$field_name])) foreach($_FILES[$input_name][$field_name] as $key => $value){
            $_FILES[$input_name.'_'.$key][$field_name] = $value;
            $keys[$key] = $key;
        }
    }
    unset($_FILES[$input_name]); // just in case
    
    foreach ($keys as $key){
    
        $new_file = $this->upload->do_upload($input_name.'_'.$key);
    
        // do your stuff with each uploaded file here, delete for example:
        $upload_data = $this->upload->data();
        unlink($upload_data['file_name']);
    
    }
    

    【讨论】:

      猜你喜欢
      • 2013-07-23
      • 2012-08-30
      • 2021-08-05
      • 1970-01-01
      • 1970-01-01
      • 2015-10-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多