如何在codeigniter中上传文件

时间:2015-05-21 10:56:41

标签: php codeigniter

我正在为我的项目使用codeigniter框架,我正在上传图片,所以我使用了一些代码。插入了其他数据,但未插入图像,我已在下面附加了我的代码 提前谢谢

HTML:

<form method="post" enctype="multipart/form-data" action="<?php echo base_url(); ?>charity/insertCharity">
<div class="control-group" style="padding:15px;">
    <label class="control-label" for="basicinput" style="padding:10px;">Logo:</label>
<div class="controls">
    <div class="input-append span6">
        <input type="file" class="span12" placeholder="Upload file" name="logo">
    </div>
</div>
</div>

控制器:

public function insertCharity(){
    $result = $this->charity_model->addCharity();
    if($result > 0){
        $data['mess'] = "Charity added successfully";
        $this->load->view('charity_view', $data);
    }
}

型号:

public function addCharity(){
    $cha_name = $this->input->post('charity_name');
    $regno = $this->input->post('reg_no');
    $contact_per = $this->input->post('contact_person');
    $contact_no = $this->input->post('contact_no');
    $address = $this->input->post('address');
    $aboutCharity = $this->input->post('about_charity');
    $createdOn = date('Y-m-d H:i:s');
    if($_FILES['logo']['size'] != 0 )
    {

        $files = $_FILES;
        $config = array();
        $config['upload_path']   =   "charity_gallery/";
        $config['allowed_types'] =   "png|jpg|gif"; 
        $config['max_size']      =   "5000";
        $this->load->library('upload',$config);
        $this->upload->initialize($config);


        $this->upload->do_upload('logo');
        $imgdata = $this->upload->data();
        $image_config=array();          
        $image_config["image_library"] = "gd2";
        $image_config['overwrite'] = TRUE;
        $image_config["source_image"] = $imgdata["full_path"];
        $image_config['create_thumb'] = FALSE;
        $image_config['maintain_ratio'] = TRUE;
        $image_config['new_image'] = $imgdata["file_path"].$imgdata["file_name"];
        $image_config['quality'] = "95%";
        $image_config['width'] = 170;
        $image_config['height'] = 170;
        $this->load->library('image_lib',$image_config);
        $this->image_lib->initialize($image_config);    
        $this->image_lib->resize();
        $logo = 'charity_gallery/'.$imgdata["file_name"];




        $query = $this->db->query("INSERT INTO `charities` (charity_name, reg_no, contact_person, contact_number, address, about, logo, created_on) VALUES ('$cha_name', '$regno', '$contact_per', '$contact_no', '$address', '$aboutCharity', '$logo', '$createdOn')");
        //$cid = $this->db->insert_id();
        $aff = $this->db->affected_rows();
        return $aff;
    }else{
        $query = $this->db->query("INSERT INTO `charities` (charity_name, reg_no, contact_person, contact_number, address, about, created_on) VALUES ('$cha_name', '$regno', '$contact_per', '$contact_no', '$address', '$aboutCharity', '$createdOn')");
        //$cid = $this->db->insert_id();
        $aff = $this->db->affected_rows();
        return $aff;
    }


}

所有值都已插入,但仅凭徽标未单独插入插入文件夹名称,如“charity_gallery”。我不知道我哪里出错了,请指导我

2 个答案:

答案 0 :(得分:2)

尝试将输入文件的name属性更改为userfile documentation

中有一个注释
  

注意:默认情况下,上传例程要求文件来自名为userfile

的表单字段


也尝试使用调试(这将打印上传错误,如果有的话):

if ( ! $this->upload->do_upload())
{
    echo $this->upload->display_errors();
    exit();
}

答案 1 :(得分:0)

其实我使用的是ubuntu机器,所以我忘了给图像文件夹的权限。在我允许后,我得到了答案