上载多个图像不存储到数据库和路径文件夹

时间:2019-01-11 08:18:14

标签: php codeigniter

我正在上传多张图片并存储到一个字段中。但是我的代码有问题,图像无法存储到数据库和路径文件夹。也许你可以帮我

这是我的控制人


    public function post() {
      if(empty($_FILES['file']['name'])) {
        $data = array(  'id_merk'                   => $this->input->post('id_merk'),
                        'createdAt'                 => $this->input->post('createdAt')
        );

        // var_dump($data);
        $this->m_barang->post( $data );
        $this->session->set_flashdata('success', 'success');
        redirect('admin/merk');
      } else {
        $count = count($_FILES['file']['size']);
        foreach($_FILES as $value){
          for($s=0; $s<=$count-1; $s++) {
            $_FILES['file']['name']     = $value['name'][$s];
            $_FILES['file']['type']     = $value['type'][$s];
            $_FILES['file']['tmp_name'] = $value['tmp_name'][$s];
            $_FILES['file']['error']    = $value['error'][$s];
            $_FILES['file']['size']     = $value['size'][$s];   
              // $config['file_name'] = 'pict_'.date('Y_m_d_H_i_s').'.jpg';
              $config['upload_path'] = './upload/be/barang';
              $config['allowed_types'] = 'gif|GIF|jpg|JPG|jpeg|JPEG|png|PNG';
              $config['max_size'] = '8000';
              $config['max_width']  = '1366';
              $config['max_height']  = '1024';
            $this->load->library('upload', $config);
            $this->upload->do_upload();

            $data = $this->upload->data();
            $name_array[] = $data['file_name'];
          }
        }
        $names = implode(', ', $name_array);

        $data = array(  'id_merk'                   => $this->input->post('id_merk'),
                        'photo_barang'              => $names
        );
        $this->m_barang->post( $data );
        $this->session->set_flashdata('success', 'gambar ada');
        redirect('admin/merk');
      }
    }

这是我的观点


    <input class="form-control" name="file[]" id="files" type="file" multiple="multiple">

请,您能帮我解决代码问题吗?

1 个答案:

答案 0 :(得分:0)

首先,仅使用1个循环即可上传多张图片。

第二,将<input type="file">的名称属性赋予$this->upload->do_upload()

第三,如果在应用程序文件夹中上载路径,则使用APPPATH.'upload/be/barang';如果在应用程序文件夹之外,则使用FCPATH.'upload/be/barang'

 $count = count($_FILES['file']['size']);
 for($s=0; $s<=$count-1; $s++) {
    $_FILES['file']['name']     = $_FILES['file']['name'][$s];
    $_FILES['file']['type']     = $_FILES['file']['type'][$s];
    $_FILES['file']['tmp_name'] = $_FILES['file']['tmp_name'][$s];
    $_FILES['file']['error']    = $_FILES['file']['error'][$s];
    $_FILES['file']['size']     = $_FILES['file']['size'][$s];   
    $config['upload_path'] = './upload/be/barang';
    $config['allowed_types'] = 'gif|GIF|jpg|JPG|jpeg|JPEG|png|PNG';
    $config['max_size'] = '8000';
    $config['max_width']  = '1366';
    $config['max_height']  = '1024';

    $this->load->library('upload', $config);
    if(!$this->upload->do_upload('file')){
        //image uploading error
    }else{
        $data = $this->upload->data();
        $name_array[] = $data['file_name'];
    }


 }