在codeigniter上传图像,是否需要用户文件?

pra*_*tri 0 html php codeigniter file-upload

我使用下面的函数将我的图像文件上传到我的服务器(Localhost).很好,图像正在上传.但是我需要两个图像字段,因此一旦点击提交按钮,就应该上传两个图像.我使用了这里描述的Codeigniter多文件上传功能,但它无法正常工作.

我收到此错误消息

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
Run Code Online (Sandbox Code Playgroud)

好吧,我无法理解错误在哪里.我用来上传单张图片的功能是

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

        }
    }
Run Code Online (Sandbox Code Playgroud)

另外我还想问我可以更改我选择的输入字段名称.即我总是需要实施<input type="file" name="userfile"/>.是否可以更改名称?我尝试更改它,我收到消息没有选择文件,所以这必须意味着我无法改变它.

Mar*_*abe 5

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

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
                }

            }
        }
}
Run Code Online (Sandbox Code Playgroud)

并尝试使用这样的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" />
Run Code Online (Sandbox Code Playgroud)

您可以根据需要更改输入字段的名称.