首页 > 解决方案 > 在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));

截屏:

在此处输入图像描述

标签: phpmysqlcodeigniter

解决方案


推荐阅读