Codeigniter can't read file name of uploaded file

时间:2017-04-10 02:49:00

标签: php codeigniter file-upload

Trying to read the file name of a file that is uploaded as part of a form. The print_r command that I'm using to test just shows a blank screen. I have read the manual (near the bottom here) pertaining to this and don't understand what I'm doing wrong.

Controller:

public function do_upload() { 
     $config['upload_path']   = './uploads/'; 
     $config['allowed_types'] = 'gif|jpg|png|txt|pdf|xlsx|csv|xls|bmp'; 
     $config['max_size']      = 1000; 

     $this->load->library('upload', $config);    
     $file_name = $this->upload->data('file_name'); 

     print_r($file_name);

View:

<?php echo form_open_multipart('Corpmuns/do_upload', array('method' => 'post'));?> 

... // some drop-down menus and text fields here

<INPUT TYPE="file" NAME="userfile" id="userfile" > 

</form>

4 个答案:

答案 0 :(得分:1)

您没有执行上传操作,因此该文件尚未上传。这就是为什么你无法获得上传文件的名称。因为它不存在。 代码$this->upload->do_upload('userfile')并确保在获取文件名之前成功上传文件。

答案 1 :(得分:0)

从上传库调用do_upload功能。

更新控制器:

public function do_upload() { 
     $config['upload_path']   = './uploads/'; 
     $config['allowed_types'] = 'gif|jpg|png|txt|pdf|xlsx|csv|xls|bmp'; 
     $config['max_size']      = 1000; 

     $this->load->library('upload', $config); 
     if ($this->upload->do_upload('userfile')) { //use this function

        $data['error'] = false;
        $upload_data = $this->upload->data();
        $data['data'] = $upload_data;
        $data['msg'] = 'Image Successfully Uploaded.';

     } else {

        $data['msg'] = $this->upload->display_errors('', '<br>');

     }

     print_r($data)
}

答案 2 :(得分:0)

        if ( ! $this->upload->do_upload('input_name'))
        {
            echo $this->upload->display_errors();
        }
        else
        {

            $file=$this->upload->data();

            echo $image=$file['file_name'];//Set file name to varilable


        }
    }

答案 3 :(得分:-1)

<form action="" enctype="multipart/form-data" method="post" 
name="uploadfile">

you should add enctype="multipart/form-data"