在CI中上载文件后获取文件名

时间:2015-04-11 10:09:52

标签: php file codeigniter

我想在单击按钮时将文件名存储在数据库中并将文件存储在文件夹中。文件已成功上传,但我没有获得文件名。 以下是我的观点:

<?php echo form_open_multipart('home/do_upload');?>
<input type="file" name="userfile" size="20" />
<br /><br />
<input type="submit" value="upload" />
</form>

这是我的控制器:

public function do_upload()
    {

        $this->load->helper(array('form', 'url'));
        $image_path = realpath(APPPATH . '../assets/pics');
        $config['upload_path'] = $image_path; //base_url('assets/pics'); //APPPATH . 'uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '100';
        $config['max_width']  = '1024';
        $config['max_height']  = '768';
        $this->load->library('upload', $config);
         $upload_data = $this->upload->data(); 
         $file_name =   $upload_data['file_name'];
        if ( ! $this->upload->do_upload())
        {
            echo 'error';
        }
        else
        {
            echo $file_name .' uploaded...';

        }

    }

2 个答案:

答案 0 :(得分:0)

在您上传的成功部分添加变量$file_data = $this-upload->data();

if ( ! $this->upload->do_upload()) {

echo 'error';

} else {

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


echo $file_data['file_name'] .' uploaded...';

}

答案 1 :(得分:0)

将输入字段名称传递给do_upload()方法:

if ( ! $this->upload->do_upload("userfile")) {
        echo 'error';
}
else {


     $upload_data = $this->upload->data(); 
     echo $upload_data['orig_name'];

     //OR pass to view
}