我想上传多张图像的最大文件是 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
)
)
把你的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>' ;
}
}