如何在mysql数据库中存储带有id的文件路径?

时间:2018-04-04 08:38:45

标签: jquery mysql ajax codeigniter

我已经使用jquery,ajax在代码点火器上完成了图片上传。它运行正常。我的问题是上传图片后,上传的图片名称存储在表格中。但我想要将图片的id与文件名一起存储图像(即)1 img1.jpg。为此我必须做的事。任何人都可以帮我解决这个问题。

Ajax添加代码:

public function ajax_add()
    {
        $this->_validate();

        $data = array(
                'firstName'     => $this->input->post('firstName'),
                'lastName'      => $this->input->post('lastName'),
                'email'         => $this->input->post('email'),
                'phone'         => $this->input->post('phone'),
                'address'       => $this->input->post('address'),
                'password'      => $this->input->post('password'),
                'status'        => $this->input->post('status'),
                'createddate'   => $this->input->post('createddate'),
                'updateddate'   => $this->input->post('updateddate'),

            );

        if(!empty($_FILES['photo']['name']))
        {
            $upload = $this->_do_upload();
            $data['photo'] = $upload;
        }

        $insert = $this->person->save($data);

        echo json_encode(array("status" => TRUE));
    }

图片上传代码:

private function _do_upload()
    {
        $config['upload_path']          = 'upload/';
        $config['allowed_types']        = 'gif|jpg|png|jpeg|pdf';
        $config['max_size']             = 1000; //set max size allowed in Kilobyte
        $config['max_width']            = 1000; // set max width image allowed
        $config['max_height']           = 1000; // set max height allowed
        $new_name = $_FILES["photo"]['name'];
        $config['file_name'] = $new_name;
        //$config['file_name']            = round(microtime(true) * 1000); //just milisecond timestamp fot unique name

        $this->load->library('upload', $config);

        if(!$this->upload->do_upload('photo')) //upload and validate
        {
            $data['inputerror'][] = 'photo';
            $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');
    }

0 个答案:

没有答案