尝试在codeigniter中上传多个文件。获取do_upload需要一个给定错误的sting数组

时间:2017-09-12 08:01:19

标签: php codeigniter codeigniter-3

我正在尝试在CodeIgniter中上传多个文件。但我收到了警告和错误

A PHP Error was encountered

Severity: Warning

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

Filename: libraries/Upload.php
Error: You did not select a file to upload.

以下是我的文件上传表单控件:

<input type="file" accept="image/png, image/jpeg, image/gif, application/pdf, application/msword, application/vnd.ms-excel, application/vnd.ms-powerpoint, text/plain, application/pdf" name="file[]" multiple/>

我的控制器功能 这里$ file是要上传的文件的名称,我从其他函数传递给这个file_upload()。

public function file_upload($file){
    $new_file = "";
    $original_file_name = '';
    if($file!=""){
        $file_name = $file;
        $original_file_name = $file_name;
        $random = rand(1, 10000000000000000);
        $makeRandom = hash('sha512', $random.$this->input->post('title') . config_item("encryption_key"));
        $file_name_rename = $makeRandom;
        $explode = explode('.', $file_name);
        if(count($explode) >= 2) {
            $new_file = $file_name_rename.'.'.$explode[1];
            $config['upload_path'] = "./uploads/images";
            $config['allowed_types'] = "gif|jpg|png|jpeg|pdf|doc|xml|docx|GIF|JPG|PNG|JPEG|PDF|DOC|XML|DOCX|xls|xlsx|txt|ppt|csv";
            $config['file_name'] = $new_file;
            $config['max_size'] = '3072';
            $config['max_width'] = '3000';
            $config['max_height'] = '3000';
             $this->load->library('upload',$config);
              $this->upload->initialize($config);
            if(!$this->upload->do_upload("file")) {
                print_r($this->upload->display_errors());
            } else {

                echo "success";


            }
        } else {
            //error
        }
    }else{
        //some code here
    }


}

1 个答案:

答案 0 :(得分:2)

imho你的代码没有任何意义 - 但这里的主要问题是 - 你要改变_FILES数组

这样的事情应该有效

public function file_upload()
{
    $strInputFileName = "file";
    $arrFiles = $_FILES;

    $config['upload_path'] = "./uploads/images";
    $config['allowed_types'] = "gif|jpg|png|jpeg|pdf|doc|xml|docx|GIF|JPG|PNG|JPEG|PDF|DOC|XML|DOCX|xls|xlsx|txt|ppt|csv";
    $config['file_name'] = $new_file;
    $config['max_size'] = '3072';
    $config['max_width'] = '3000';
    $config['max_height'] = '3000';
    $config['file_name'] = $this->getRandomFileName();

    if (is_array($_FILES[$strInputFileName]['name']))
    {
        $countFiles = count($_FILES[$strInputFileName]['name']);
        for($i=0;$i<$countFiles; $i++)
        {
            //overwrite _FILES array
            $_FILES[$strInputFileName]['name'] = $arrFiles[$strInputFileName]['name'][$i];
            $_FILES[$strInputFileName]['type'] = $arrFiles[$strInputFileName]['type'][$i];
            $_FILES[$strInputFileName]['tmp_name'] = $arrFiles$strInputFileName]['tmp_name'][$i];
            $_FILES[$strInputFileName]['error'] = $arrFiles[$strInputFileName]['error'][$i];
            $_FILES[$strInputFileName]['size'] = $arrFiles[$strInputFileName]['size'][$i];
            $this->upload->initialize($config);
            if(!$this->upload->do_upload($strInputFileName)) 
            {
                print_r($this->upload->display_errors());
            } 
            else 
            {
                echo "success";
            }
        }
    }
    else
    {
        $this->upload->initialize($config);
        if(!$this->upload->do_upload($strInputFileName)) 
        {
            print_r($this->upload->display_errors());
        } 
        else 
        {
            echo "success";
        }
    }
}

private function getRandomFileName()
{
    $random = rand(1, 10000000000000000);
    return hash('sha512', $random.$this->input->post('title') . config_item("encryption_key"));
}