在 codeigniter 中上传图像并插入文本

发布于 2024-12-01 05:36:48 字数 1018 浏览 0 评论 0原文

我正在尝试上传带有标题和描述的图像以及图像上传。我的图像上传和文本输入是分开工作的,文本和图像路径也被插入到数据库中,但我仍然无法弄清楚如何将两者结合起来,

我使用了 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);
        }
    }

有人可以告诉我如何在图像中插入一些文本字段将表单上传到sql数据库?

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

i used the guides given in 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情绪少女 2024-12-08 05:36:48

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

// 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 读取:

// 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);
    }
}

您还需要制作一个适当的模型来将数据输入数据库中。

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文