Codeigniter-上传图像后将批次插入数据库

时间:2018-09-01 02:43:26

标签: php codeigniter

我要上传多张图片,最大文件为2 mb,它可以工作,但是当我尝试插入数据库时​​失败。我已经尝试过,但是找不到解决此问题的方法。这是代码:

控制器

function add_portfolio(){

$this->load->library('upload');
$dataInfo = array();
$image = array();
$files = $_FILES;
$cpt = count($_FILES['userfile']['name']);
for($i=0; $i<$cpt; $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];

    $this->upload->initialize($this->set_upload_options());
    $this->upload->do_upload('userfile');
    $dataInfo[] = $this->upload->data();

    if ( ! $this->upload->do_upload('userfile'))
            {
              echo    $error = $this->upload->display_errors();
            }
            else
            {

               $image[] = array(
                 'id_relasi' => $id,
                 'jenis_image' => 'portfolio',
                 'image' => $dataInfo[$i]['file_name']
                 );


                  $this->db->insert_batch('image', $image);
                   redirect(base_url('Worker/Akun/'));
                   // echo '<pre>' ;
                   // print_r($image);
                   // echo '</pre>' ;
                }
    }
    }

配置映像

private function set_upload_options(){
    $config = array();
    $config['upload_path'] = 'assets/user/images/img/'; 
    $config['remove_spaces']=TRUE;
    $config['encrypt_name'] = TRUE; 
    $config['allowed_types'] = 'gif|jpg|png|JPEG|jpeg';
    $config['max_size'] = '2097152';
    $config['overwrite'] = FALSE;
    return $config;
}

但是我想数组有问题。

要插入批处理数组,必须这样

$data = array(
        array(
                'title' => 'My title',
                'name' => 'My Name',
                'date' => 'My date'
        ),
        array(
                'title' => 'Another title',
                'name' => 'Another Name',
                'date' => 'Another date'
        )
);

,这是我上传3张图片时的数组:

Array
(
    [0] => Array
        (
            [id_relasi] => 1379108884
            [jenis_image] => portfolio
            [image] => 0b91d67888c164809b0ff5b38871a685.jpg
        )

)

Array
(
    [0] => Array
        (
            [id_relasi] => 1379108884
            [jenis_image] => portfolio
            [image] => 0b91d67888c164809b0ff5b38871a685.jpg
        )

    [1] => Array
        (
            [id_relasi] => 1913645310
            [jenis_image] => portfolio
            [image] => 5d29df6d3eff731aa408de47767225b6.jpg
        )

)

Array
(
    [0] => Array
        (
            [id_relasi] => 1379108884
            [jenis_image] => portfolio
            [image] => 0b91d67888c164809b0ff5b38871a685.jpg
        )

    [1] => Array
        (
            [id_relasi] => 1913645310
            [jenis_image] => portfolio
            [image] => 5d29df6d3eff731aa408de47767225b6.jpg
        )

    [2] => Array
        (
            [id_relasi] => 31208956
            [jenis_image] => portfolio
            [image] => 3bea22e3ef638046f082a64a2f27e6e6.png
        )

)

1 个答案:

答案 0 :(得分:1)

将您的insert_batch带到for循环之外。

function add_portfolio(){

$this->load->library('upload');
$dataInfo = array();
$image = array();
$files = $_FILES;
$cpt = count($_FILES['userfile']['name']);
$error= [];
for($i=0; $i<$cpt; $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];

    $this->upload->initialize($this->set_upload_options());
    $this->upload->do_upload('userfile');
    $dataInfo[] = $this->upload->data();

    $image[] = array(
                 'id_relasi' => $id,
                 'jenis_image' => 'portfolio',
                 'image' => $dataInfo[$i]['file_name']
                 );


    if ( ! $this->upload->do_upload('userfile'))
    {
              $error[] =  $this->upload->display_errors();
    }
  }

  //outside for loop

  if(!empty($uploaded)) {
    echo explode('<br>',$error);
  }
  else {
    $this->db->insert_batch('image', $image);
    redirect(base_url('Worker/Akun/'));
    // echo '<pre>' ;
    // print_r($image);
    // echo '</pre>' ;
  }
}