0

这是表单动作或其他事情

<?php
echo form_open_multipart("/Register/registerformModel",
                         array("name"=>"subform2","id"=>"subform2"));
?>

这是 HTML 文件

<input name="userfile" id="userfile" type="file" class="input"/>

这是用于将图像文件插入数据库的功能

public function registerformModel()
{
    $this->load->model("common_model");
    $config['upload_path'] = $this->config->item('upload_url_path').'images/member/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['file_name'] = date('Ymd_his_').rand(10,99).rand(10,99).rand(10,99);
    $config['overwrite'] = FALSE;
    $config['max_size'] = '0';
    $config['max_width'] = '0';
    $config['max_height'] = '0';
    $config['max_filename'] = '0';
    $config['remove_spaces'] = FALSE;
    $this->load->library('upload', $config);

    if ( ! $this->upload->do_upload())
    {
        $data['upload_data']['file_name'] = '';
        echo $this->upload->display_errors('<p style="color:#FF0000;">','</p>');                    
    }
    else
    {
        $data = array('upload_data' => $this->upload->data());
        $config['image_library'] = 'gd2';
        $config['source_image'] = $config['upload_path'].$data['upload_data']['file_name'];
        $config['quality'] = '100%';
        $config['width'] = 50;
        $config['height'] = 50;
        $config['create_thumb'] = FALSE;
        $config['maintain_ratio'] = TRUE;
        $this->load->library('image_library', $config);
        $this->image_lib->resize();
    }

    $data['query'] = array(
            'image' => $data['upload_data']['file_name']
            );  

    $num = $this->common_model->insert('member',$data['query']);
    $msg = ($num > 0) ? $this->lang->line('added') : $this->lang->line('not_added');
    $this->session->set_flashdata('msg', $msg);         
}

查询运行成功,但未插入图像。

4

1 回答 1

1

你能把这行 $this->upload->do_upload() 换成 $this->upload->do_upload( 'userfile' )

如果您想将调整大小的图像保存到任何文件夹,请写 $config['new_image'] = "path to save resized image";

我已经尝试过这段代码,它对我来说工作正常。

      public function registerformModel(){
        $this->load->model("common_model");
        $config['upload_path'] = 'uploads/test';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['file_name'] = date('Ymd_his_').rand(10,99).rand(10,99).rand(10,99);
        $config['overwrite'] = FALSE;
        $config['max_size'] = '0';
        $config['max_width']  = '0';
        $config['max_height']  = '0';
        $config['max_filename']  = '0';
        $config['remove_spaces']  = FALSE;
        $this->load->library('upload', $config);

        if ( ! $this->upload->do_upload())
        {
            $data['upload_data']['file_name'] = '';
            echo $this->upload->display_errors('<p style="color:#FF0000;">','</p>');                    
        }
        else
        { 
        $data = array('upload_data' => $this->upload->data());
        $config['image_library'] = 'gd2';
        $config['source_image'] = $config['upload_path'].$data['upload_data']['file_name'];
        $config['quality'] = '100%';
        $config['width'] = 50;
        $config['height'] = 50;
        $config['create_thumb'] = FALSE;
        $config['maintain_ratio'] = TRUE;
        $this->load->library(**'image_lib'**, $config);
        $this->image_lib->resize();
        }

        $data['query'] = array(
                'image' => $data['upload_data']['file_name']
                );  

        $num = $this->common_model->insert('member',$data['query']);
        $msg = ($num > 0) ? $this->lang->line('added') : $this->lang->line('not_added');
        $this->session->set_flashdata('msg', $msg);         
        }

一切顺利

于 2013-10-30T07:04:55.127 回答