【问题标题】:CodeIgniter - Uploading an Image through a form, store the location of the image in databaseCodeIgniter - 通过表单上传图像,将图像的位置存储在数据库中
【发布时间】:2010-09-22 05:33:15
【问题描述】:

我正在尝试通过表单将图像上传到我的网站,但是将图像的位置存储在数据库中(而不是让数据库陷入困境)效率更高。

我的表格有问题,真的不知道该去哪里:

<?=form_open('bro/submit_new');?>
 //other form data
 Image: <input type="file" name="image" size="20" /> <br>
 <input type="submit" value="Submit" />
</form>

现在表单本身工作正常,问题是它试图将图像存储到数据库字段“图像”(TEXT 类型)中。告诉它只存储文件并将文件位置存储在“图像”字段中的最简单方法是什么? (我告诉它通过控制器上传文件的位置)。

谢谢

编辑:控制器代码(本部分):

function submit_new(){
    $config['upload_path'] = './images/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '2000';
    $config['max_width']  = '1024';
    $config['max_height']  = '768';     
    $this->load->library('upload', $config);

    $this->db->insert('post', $_POST);

    redirect('bro');
}

【问题讨论】:

    标签: php mysql file codeigniter upload


    【解决方案1】:

    CodeIgniter 的文件上传类会为你做这件事。 entry in their user guide 尽我所能解释,所以我将向您指出。

    基本上你只需要修改他们在那里的控制器,包括一些你将文件 URL 放入数据库的位置,你可以通过使用 $this->upload->data() 并提取[full_path] 从结果数组中提取,然后将其发送到处理数据库输入的模型。

    【讨论】:

    • 我的印象是它直接将文件加载到数据库中?我有错吗?
    • 但我想我没有想到为什么它会将它加载到两个位置
    • 不,它会将其上传到您指定的任何目录 :) 阅读用户指南!呵呵。一点都不傻。用户指南有点庞大,每个人都可以了解有关 CodeIgniter 的所有信息。
    【解决方案2】:

    使用 form_open_multipart() 而不是 form_open()

    【讨论】:

      【解决方案3】:

      使用模型页面将数据上传到数据库

      【讨论】:

        猜你喜欢
        • 2016-09-06
        • 2011-02-16
        • 1970-01-01
        • 1970-01-01
        • 2017-02-15
        • 1970-01-01
        • 2011-05-25
        • 2015-04-15
        • 1970-01-01
        相关资源
        最近更新 更多