图像未在codeigniter中上传

时间:2013-10-30 06:55:18

标签: php codeigniter

这是表格行动或其他事情

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

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

1 个答案:

答案 0 :(得分:1)

  

您可以将此行$ this-&gt; upload-&gt; do_upload()替换为$ this-&gt; upload-&gt; do_upload('userfile'

如果您想将调整大小图像保存到任何文件夹,请编写$ config ['new_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);         
        }

所有最好的