Code Igniter中的多个ImageUpload

时间:2016-12-13 05:16:57

标签: php codeigniter image-uploading

我在codeigniter上传多张图片时遇到问题

MyView的

<tr>
                <td> Picture</td>
                <td>:</td>
                <td><input type="file" name="userfile[]" multiple /></input></td>
            </tr>

和我的模特

function multi_upload_files($imagepath=''){

     $number_of_files_uploaded = count($_FILES['userfile']['name']);

       for ($i = 0; $i < $number_of_files_uploaded; $i++) {
      $_FILES['userfile']['name']     = $_FILES['userfile']['name'][$i];
      $_FILES['userfile']['type']     = $_FILES['userfile']['type'][$i];
      $_FILES['userfile']['tmp_name'] = $_FILES['userfile']['tmp_name'][$i];
      $_FILES['userfile']['error']    = $_FILES['userfile']['error'][$i];
      $_FILES['userfile']['size']     = $_FILES['userfile']['size'][$i];


    $config['upload_path'] = $imagepath;
    $config['allowed_types'] = 'gif|jpg|jpeg|png';
    $config['max_size'] = 900;
    $config['max_width'] = 2024;
    $config['max_height'] = 1068;

    $this->upload->initialize($config);

     $fileData = $this->upload->data();
     $uploadData[$i]['file_name'] = $fileData['file_name'];
     $images[]= $uploadData[$i]['file_name'];
     return $images;
    }  }  

我已经测试过了,这个代码只是读了我上传的问题图片只有1张图片,不等于刚上传的文件编号(例如:2张图片)

2 个答案:

答案 0 :(得分:0)

如果您查看以下几行,您只需使用数组中的第一项覆盖所有内容。因此,只处理第一个项目,其余项目被覆盖。另外,你在循环结束时有一个回报。该函数在循环完成之前返回。

  $_FILES['userfile']['name']     = $_FILES['userfile']['name'][$i];
  $_FILES['userfile']['type']     = $_FILES['userfile']['type'][$i];
  $_FILES['userfile']['tmp_name'] = $_FILES['userfile']['tmp_name'][$i];
  $_FILES['userfile']['error']    = $_FILES['userfile']['error'][$i];
  $_FILES['userfile']['size']     = $_FILES['userfile']['size'][$i];
  ...
  return $images;

答案 1 :(得分:0)

你错过了两件事。

首先,未定义变量$imagepath

其次,你错过了加载upload libaray。

所以,解决方案就在这里试试吧。希望它能起作用......

function multi_upload_files($imagepath=''){
         $data = array();
         $number_of_files_uploaded = count($_FILES['userfile']['name']);

           for ($i = 0; $i < $number_of_files_uploaded; $i++) {
          $_FILES['userfile']['name']     = $_FILES['userfile']['name'][$i];
          $_FILES['userfile']['type']     = $_FILES['userfile']['type'][$i];
          $_FILES['userfile']['tmp_name'] = $_FILES['userfile']['tmp_name'][$i];
          $_FILES['userfile']['error']    = $_FILES['userfile']['error'][$i];
          $_FILES['userfile']['size']     = $_FILES['userfile']['size'][$i];

        $imagepath = 'Define upload path here';
        $config['upload_path'] = $imagepath;
        $config['allowed_types'] = 'gif|jpg|jpeg|png';
        $config['max_size'] = 900;
        $config['max_width'] = 2024;
        $config['max_height'] = 1068;
        //Load library first then use
        $this->load->library('upload');
        $this->upload->initialize($config);
         if($this->upload->do_upload('userfile'))
          {
            $fileData = $this->upload->data();
            $uploadData[$i]['file_name'] = $fileData['file_name'];
            $images[]= $uploadData[$i]['file_name'];
          }  
         return $images;
    }