如何在codeigniter中获取上传文件名

时间:2016-09-05 08:31:26

标签: codeigniter upload

您好我正在尝试将图片上传到数据库中的文件夹和图片名称

在视图中

echo form_open_multipart('subscribers_c/actioncreate',array('class'=>'form'));`enter code here`
<input type='file' name='img'>
控制器中的

$config['upload_path'] ='./assets/uploads/subscribers_photos'; //The path where the image will be save
        $config['allowed_types'] = 'gif|jpg|png'; //Images extensions accepted
        $config['max_size']    = '2048'; 
        $config['max_width']  = '1024'; 
        $config['max_height']  = '768'; 
        $config['overwrite'] = TRUE; 
        $this->load->library('upload', $config); //Load the upload CI library
        if (!$this->upload->do_upload('img'))
        {
            $uploadError = array('upload_error' => $this->upload->display_errors()); 
            //$this->set_flashdata('msg_error', $uploadError, site_url().'/subscribers_c/actioncreate'); 
        }
        $file_info = $this->upload->data('img');
        $img_name = $file_info['file_name']; 

        $data=array(
            'chit_fund_id'=>$this->input->post('cid'),
            'first_name'=>$this->input->post('fname'),
            'last_name'=>$this->input->post('lname'),
            'dob'=>$this->input->post('bd'),
            'gender'=>$this->input->post('g'),
            'contact_number'=>$this->input->post('mob'),
            'address'=>$this->input->post('add'),
            'email_id'=>$this->input->post('eml'),
            'bid_status'=>$this->input->post('b_status'),
            'user_status'=>$this->input->post('u_status'),
            'image_name'=>$img_name,
        );
        //print_r($data);

image_name没有任何值 如何获取图像名称。

3 个答案:

答案 0 :(得分:1)

而不是img in upload data

$file_info = $this->upload->data('img');

尝试

$file_info = $this->upload->data();
$img = $file_info['file_name']; 

此处$config['upload_path'] = './assets/uploads/subscribers_photos';/

结束

$config['upload_path'] = './assets/uploads/subscribers_photos/';

答案 1 :(得分:0)

在视图内部调用form_upload函数(如果需要,你的那个也是正确的):

<?php echo form_upload('pic'); ?>  

用于上传的内部控制器功能:

$img_name = $_FILES["pic"]["name"];  

通过使用此代码,您将获得文件名。

答案 2 :(得分:0)

$img_name = $this->upload->data('file_name');