为什么多次上传没有插入数据库

时间:2017-08-26 05:44:29

标签: php mysql image-uploading codeigniter-3

这里我试图将多个图像插入数据库但不插入它们。图像正确上传到文件夹,但不知道为什么它没有进入数据库。 这是我的图片上传功能

public function multiple_upload_files($path)
{
    $images = array();
    if(!empty($_FILES['files']['name'])){
        $filesCount = count($_FILES['files']['name']);
        for($i = 0; $i < $filesCount; $i++){
          $_FILES['file']['name'] = $_FILES['files']['name'][$i];
          $_FILES['file']['type'] = $_FILES['files']['type'][$i];
          $_FILES['file']['tmp_name'] = $_FILES['files']['tmp_name'][$i];
          $_FILES['file']['error'] = $_FILES['files']['error'][$i];
          $_FILES['file']['size'] = $_FILES['files']['size'][$i];
          $config['upload_path']= './uploads/'.$path.'/';
          $config['allowed_types'] = 'gif|jpg|png|jpeg';
          $config['max_size']= '2000';
          $config['max_width'] = '4000';
          $config['max_height'] = '6500';
          $config['file_name']='upld-file'.time();
          $this->load->library('upload', $config);
          $this->upload->initialize($config);
         if ($this->upload->do_upload("file")) {

           $images[] = $this->upload->data();
        }
        else {  
         redirect('admin/view-product');

        }
    }
           return $images;
}

这是我的插入代码

$path='products';
     if($this->multiple_upload_files($path))
     {
      $img=implode(',',$images);
      $data = array('product_name' => $this->input->post('product_name'),'image'=>$img);
      $status = $this->Admin_model->db_insert($table='products',$data);
        if($status)
        {
          $this->session->set_flashdata('message','Product added Successfully');
        }
        else
        {
          $this->session->set_flashdata('message','Insertion failed');
        }

问题是图像是上传到上传的文件夹而不是数据库。

1 个答案:

答案 0 :(得分:2)

您正在从方法返回图像数组但未接收它。改变你的代码如下

 $path='products';
 $images= $this->multiple_upload_files($path);
 if($images)
 {
  $img=implode(',',$images);
  $data = array('product_name' => $this->input->post('product_name'),'image'=>$img);
  $status = $this->Admin_model->db_insert($table='products',$data);
    if($status)
    {
      $this->session->set_flashdata('message','Product added Successfully');
    }
    else
    {
      $this->session->set_flashdata('message','Insertion failed');
    }