php - 在codeigniter上上传图片和视频
问题描述
当我在数据库中插入图像和视频时遇到一些问题,我正在使用 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));
截屏:
解决方案
推荐阅读
- python-3.x - 如何使用 LINE Notify 仅发送图像
- javascript - Symfony 4 - 如何在 javascript 中使用资产?
- c# - 如何使用位图向多个图像添加水印
- python - 使用实例方法时带莳萝的 Pathos KeyError
- d3.js - 在 BigQuery 中订购大型时间序列数据集以进行导出
- javascript - 仅在用户交互但未获取隐藏输入字段中的值时触发加载recaptcha库
- r - 根据列表项的行拆分列表
- c++ - 为自定义打印调试实现传递任何类型的变量
- python - 每个分位数和预设四分位数的 Python 计数
- python - Pandas Groupby 生成一个单独的数据框