上传图像并在codeigniter中插入文本 [英] upload image and insert text in codeigniter

查看:75
本文介绍了上传图像并在codeigniter中插入文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试上传包含标题和说明的图片以及图片上传。我的图片上传和文本输入单独工作,文本和图片路径也插入到数据库,但我仍然无法如何结合两个

im trying to upload an image with a title and description along with the image upload. My image upload and text inputs work separately and the text and the image path is also inserted to the DB but i still cant figure how to combine both

我使用指南位于 http://codeigniter.com/user_guide/libraries/file_uploading.html

function do_upload()
    {
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '100';
        $config['max_width']  = '1024';
        $config['max_height']  = '768';

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

        if ( ! $this->upload->do_upload())
        {
            $error = array('error' => $this->upload->display_errors());

            $this->load->view('upload_form', $error);
        }
        else
        {
            $data = array('upload_data' => $this->upload->data());

            $this->load->view('upload_success', $data);
        }
    }

有人可以告诉我如何插入一些文本字段

can someone tell me how can i insert some text fields along with the image upload form to a sql database?

推荐答案

基本上,只需将图像上传输入以相同的形式,例如:

Basically, just put the image upload input in the same form, like:

// view
echo form_open_multipart('controller_name/do_upload');
echo form_input("title", "");
echo form_input("description", "");
echo form_upload("userfile");
echo form_close

并在控制器中添加POST读数:

And add in the controller the POST reading:

// controller
function do_upload()
{
    $config['upload_path'] = './uploads/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '100';
    $config['max_width']  = '1024';
    $config['max_height']  = '768';

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

    // validate the POST data 
    // http://codeigniter.com/user_guide/libraries/form_validation.html
    $this->form_validation->set_rules('title', 'Title', 'trim|required');
    $this->form_validation->set_rules('description', 'Description', 'trim|required')

    if ($this->form_validation->run() == FALSE)
    {   
        // failed validation
        $this->load->view('myform');

        // quit here
        return false;
    }

    if ( ! $this->upload->do_upload())
    {
        // no file uploaded or failed upload
        $error = array('error' => $this->upload->display_errors());
        $this->load->view('upload_form', $error);
    }
    else
    {
        // success
        $data = array('upload_data' => $this->upload->data());
        $title = $this->input->post('title');
        $description = $this->input->post('description');

        // a model that deals with your image data (you have to create this)
        $this->your_upload_model->add($title, $description, $data["file_name"]);

        $this->load->view('upload_success', $data);
    }
}

您还需要输入正确的模型数据库中的数据。

You will also need to make a proper model to input the data in the database.

这篇关于上传图像并在codeigniter中插入文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆