【问题标题】:How to save image in my database?如何将图像保存在我的数据库中?
【发布时间】:2014-03-01 14:14:25
【问题描述】:

在我的页面中,我创建了一个包含不同字段(如姓名、电子邮件、密码等)的表单,现在我必须上传图像并存储在我的数据库中。任何人发送包含图像上传文件在内的所有字段的代码。我必须尝试很多次,图像无法上传到我的数据库中。

我的控制器是

function activity() {
    $this->load->library('form_validation');
    //field name,error message, validation rules
    $this->form_validation->set_rules('activityid', 'Activity Id', 'trim|required');
    $this->form_validation->set_rules('hostid', 'Host Id', 'trim|required');
    $this->form_validation->set_rules('activityname', 'Activity Name', 'trim|required');
    $this->form_validation->set_rules('date', 'Date', 'trim|required');
    $this->form_validation->set_rules('venue', 'Venue', 'trim|required');
    $this->form_validation->set_rules('typeofactivity', 'Type of Activity', 'trim|required');
    $this->form_validation->set_rules('conductedby', 'Conducted By', 'trim|required');
    if ($this->form_validation->run() == FALSE) {
        //$this->load->view('signup_form');
        $this->activity_reg();
    } else {
        $this->load->model('membership_model');

        $query = $this->membership_model->activity();

        if ($query) {
            $data['main_content'] = 'signup_successful';
            $this->load->view('includes/templates', $data);
        } else {
            $this->load->view('activity');
        }
    }
}

我的模特是

function activity() {
    $new_member_insert_data = array(
        'activityid' => $this->input->post('activityid'),
        'hostid' => $this->input->post('hostid'),
        'activityname' => $this->input->post('activityname'),
        'date' => $this->input->post('date'),
        'venue' => $this->input->post('venue'),
        'typeofactivity' => $this->input->post('typeofactivity'),
        'conductedby' => $this->input->post('conductedby')
    );

    $this->load->database();
    $insert = $this->db->insert('activity', $new_member_insert_data);
    return $insert;
}

【问题讨论】:

标签: php codeigniter


【解决方案1】:

您通常最好将图像存储在文件系统中并将PATH 作为文本保存到数据库中的图像中。

如果您不喜欢使用数据库,则需要将图像插入为BLOB.

这里有一个很好的概述和教程:http://www.phpro.org/tutorials/Storing-Images-in-MySQL-with-PHP.html#3

【讨论】:

    【解决方案2】:

    您不必将图像保存在数据库中。 以下是使您的应用程序安全的方法: 当您上传图片时:

    • 验证图片
    • 在变量中保护其名称
    • 将其重命名为标准名称
    • 将其上传到安全文件夹中
    • 将图像的原始名称和重命名的名称存储在数据库中(而不是图像本身)
    • 检索图像时,只需定义图像的路径并回显新名称即可。

    这应该可以正常工作

    或者,如果您真的想将图像插入数据库,请关注此页面。它应该给你你想要的!

    http://mrarrowhead.com/index.php?page=store_images_mysql_php.php

    【讨论】:

      猜你喜欢
      • 2015-01-08
      • 2013-02-09
      • 2013-10-04
      • 1970-01-01
      • 1970-01-01
      • 2015-12-17
      • 2020-04-07
      • 1970-01-01
      • 2019-12-23
      相关资源
      最近更新 更多