dongpobo6009 2013-02-13 21:09
浏览 65
已采纳

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

I am using the function below to upload my image files to my server(Localhost). It is fine and image is being uploaded. But I need two image fields and hence both the images should be uploaded once the submit button is clicked. I used the function described here Codeigniter multiple file upload , but it is not working.

I get this error message

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

Well I cannot understand where the error is. The function that I am using to upload single image is

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

        }
    }

In addition I also like to ask can i change the input field name of my choice. i.e i always need to implement <input type="file" name="userfile"/> . Is it possible to change the name? I tried changing it and I get the message No file was selected, so that must mean that I cannot change it.

  • 写回答

2条回答 默认 最新

  • doog1092 2013-02-13 21:15
    关注

    You have to loop through the uploaded files like it is shown in your provided link.

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

    And try using HTML like this:

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

    You can change the names of the input fields as you like.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 怎么改成循环输入删除(语言-c语言)
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件
  • ¥15 使用ESP8266连接阿里云出现问题