猿问

你如何在代码点火器中将图像上传到数据库中?

我一直在尝试将图像上传到我的数据库(注意:我使用的是 Codeigniter 3)但是每次我尝试将图像上传到数据库时,它都不会存储它的文件名和图像本身,并且只存储 1 位在 BLOB var 类型中。


$photo = file_get_contents($_FILES['image']['tmp_name];

this->db->select('*')


[...]


if(empty($response){


   $data['entry_phone_no'] = $this->input->post('phone_no');

   $data['entry_email'] = $this->input->post('email');

   $data['entry_firstname'] = $this->input->post('first_name');

   $data['entry_lastname'] = $this->input->post('last_name');

   $data['entry_photo'] = $photo;

   $data['entry_photo_name'] = $photo;

在我的 model.php 中,我有上面的设置,它保存了其他数据(电话号码、电子邮件等)。我知道我在这"$photo"部分遗漏了一些代码,但有人可以指出吗?


暮色呼如
浏览 129回答 1
1回答

慕村9548890

您可以将图像上传到名为“Uploads”的文件夹中,并将该文件名保存到数据库中,如下所示:视图.php:<input class="my-set_3" type="file" name="chatbot_profile" id="chatbot_profile" required>控制器.php:if (!empty($_FILES['chatbot_profile']['name'])){&nbsp; $config['upload_path']&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; = './uploads';&nbsp; $config['allowed_types']&nbsp; &nbsp; &nbsp; &nbsp; = 'gif|jpg|png';&nbsp; $config['max_size']&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;= 1000000;&nbsp; $config['file_name']&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=&nbsp; time();&nbsp; $this->load->library('upload', $config);&nbsp; if ( ! $this->upload->do_upload('chatbot_profile'))&nbsp; {&nbsp; &nbsp; &nbsp; $error = array('error' => $this->upload->display_errors());&nbsp; }&nbsp; else&nbsp; {&nbsp; &nbsp; &nbsp; $data = array('upload_data' => $this->upload->data());&nbsp; &nbsp; &nbsp; $file_name = $data['upload_data']['file_name'] ;&nbsp; &nbsp; &nbsp; // Store this filename into database&nbsp; &nbsp; &nbsp; // Your database code will be placed here&nbsp; &nbsp; &nbsp; // OR call model&nbsp; mathod from here&nbsp; }}注意:uploads 文件夹将由我们在应用程序文件夹之外创建
随时随地看视频慕课网APP
我要回答