当我在数据库中插入图像和视频时,我遇到了一些问题,我正在使用codeigniter进行编程。
当我同时插入视频和图像时,我的代码不起作用,但是如果我仅上传图像/视频中的一个,则效果很好。
如何解决插入视频和图像的问题?
我的控制器代码:
private function _do_upload()
{
$config['upload_path'] = './assets/img/sni';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 3000; //set max size allowed in Kilobyte
$config['max_width'] = 1000; // set max width image allowed
$config['max_height'] = 1000; // set max height allowed
$config['file_name'] = round(microtime(true) * 1000); //just milisecond timestamp fot unique name
$this->load->library('upload', $config);
if (!$this->upload->do_upload('image')) //upload and validate
{
$data['inputerror'][] = 'image';
$data['error_string'][] = 'Upload error: ' . $this->upload->display_errors('', ''); //show ajax error
$data['status'] = FALSE;
echo json_encode($data);
exit();
}
return $this->upload->data('file_name');
}
private function _do_upload_video()
{
$config['upload_path'] = './assets/img/sni';
$config['allowed_types'] = 'mp4';
$config['max_size'] = 9000148; //set max size allowed in Kilobyte
$config['file_name'] = uniqid(); //just milisecond timestamp fot unique name
$this->load->library('upload', $config);
if (!$this->upload->do_upload('video')) //upload and validate
{
$data['inputerror'][] = 'video';
$data['error_string'][] = 'Upload error: ' . $this->upload->display_errors('', ''); //show ajax error
$data['status'] = FALSE;
echo json_encode($data);
exit();
}
return $this->upload->data('file_name');
}
public function add_data()
{
$this->_validate();
$data = array(
'kode_process' => $this->input->post('kode'),
'process_name' => $this->input->post('name'),
'remark' => $this->input->post('remark'),
'category' => $this->input->post('category'),
'cycle_time' => $this->input->post('cycle'),
'smv' => $this->input->post('smv'),
'total' => $this->input->post('total'),
);
if (!empty($_FILES['image']['name'])) {
$upload = $this->_do_upload();
$data['image'] = $upload;
}
if (!empty($_FILES['video']['name'])) {
$upload = $this->_do_upload_video();
$data['video'] = $upload;
}
$this->Sni_model->save($data);
echo json_encode(array("status" => TRUE));
屏幕截图: