在 Codeigniter 中如何将图像上传到数据库?
How do you upload image into database in Code igniter?
我一直在尝试将图像上传到我的数据库(注意:我使用的是 Codeigniter 3)但是每次我尝试将图像上传到数据库时它都不会存储它的文件名和图像本身并且在 BLOB var 类型中只存储 1 位。
$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 中,我有上面的设置,它保存了其他数据(phone 号码、电子邮件等)。我知道我在 "$photo"
部分遗漏了一些代码,但有人可以指出吗?
您可以将图片上传到名为 "Uploads" 的文件夹中,并将该文件名保存到数据库中,如下所示:
Views.php:
<input class="my-set_3" type="file" name="chatbot_profile" id="chatbot_profile" required>
Controller.php:
if (!empty($_FILES['chatbot_profile']['name']))
{
$config['upload_path'] = './uploads';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 1000000;
$config['file_name'] = time();
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('chatbot_profile'))
{
$error = array('error' => $this->upload->display_errors());
}
else
{
$data = array('upload_data' => $this->upload->data());
$file_name = $data['upload_data']['file_name'] ;
// Store this filename into database
// Your database code will be placed here
// OR call model mathod from here
}
}
Note: uploads folder will created by us and outside the application
folder.
我一直在尝试将图像上传到我的数据库(注意:我使用的是 Codeigniter 3)但是每次我尝试将图像上传到数据库时它都不会存储它的文件名和图像本身并且在 BLOB var 类型中只存储 1 位。
$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 中,我有上面的设置,它保存了其他数据(phone 号码、电子邮件等)。我知道我在 "$photo"
部分遗漏了一些代码,但有人可以指出吗?
您可以将图片上传到名为 "Uploads" 的文件夹中,并将该文件名保存到数据库中,如下所示:
Views.php:
<input class="my-set_3" type="file" name="chatbot_profile" id="chatbot_profile" required>
Controller.php:
if (!empty($_FILES['chatbot_profile']['name']))
{
$config['upload_path'] = './uploads';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 1000000;
$config['file_name'] = time();
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('chatbot_profile'))
{
$error = array('error' => $this->upload->display_errors());
}
else
{
$data = array('upload_data' => $this->upload->data());
$file_name = $data['upload_data']['file_name'] ;
// Store this filename into database
// Your database code will be placed here
// OR call model mathod from here
}
}
Note: uploads folder will created by us and outside the application folder.