使用post方法在代码点火器中上传文件

时间:2018-03-27 08:28:22

标签: php codeigniter file-upload

我必须使用带有POST方法的表单在CodeIgniter中上传文件。但是,我必须在我的控制器中使用Codeigniter的文件上传技术。为此,我编写了以下代码,但它没有上传文件并显示 No File was selected 的错误。请说出可能存在的问题?

代码:

/* HTML FORM INSIDE VIEW */
<form action="<?php echo base_url().'index.php/transferdata/uploadfile';?>" method="post" id="uploadForm">
    <input type="file" name="userfile" id="userfile" style="display: none;" onchange="document.getElementById('submitfile').click();" />
    <a class="toolbar" href="#" onclick="document.getElementById('userfile').click();">
        <span title="Upload" class="icon-32-upload"></span>
        Upload
    </a>
    <input type="submit" name="submitfile" id="submitfile" style="display: none;" />
</form> 


/* CODE-IGNITER CONTROLLER at index.php/transferdata/uploadfile */
$config['upload_path']   = './uploads/'; 
$config['allowed_types'] = 'xls|xlsx'; 
$this->load->library('upload', $config);

if ( ! $this->upload->do_upload($_POST["userfile"])) {
    $this->session->set_flashdata('upload_response',$this->upload->display_errors());
    redirect(base_url()."index.php/transferdata"); 
    return;
}

$file_details = array('upload_data' => $this->upload->data());
$file_path = $file_details['upload_data']['full_path'];

2 个答案:

答案 0 :(得分:2)

每当您上传任何文件时,您必须在表单中设置enctype="multipart/form-data" 因此,请将表单标记更改为:

<form action="<?php echo base_url().'index.php/transferdata/uploadfile';?>" method="post" id="uploadForm" enctype="multipart/form-data">

同样在codeigniter控制器中更改do_upload方法,如下所示:

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

答案 1 :(得分:2)

插入表单标记中的代码:enctype="multipart/form-data"

并在此处更改$this->upload->do_upload("userfile");