文件上传到数据库

时间:2018-12-23 15:38:37

标签: php mysql codeigniter file-upload

在用户填写表格后出现错误,我正在上传文件及其详细信息。我需要在代码中的哪个位置进行调整,以使文件成功上传?

这是用于将音频文件上载到上载文件夹,然后将其名称添加到数据库,以及来自用户的其他表单输入详细信息。

public function music_upload_success(){
    if($this->logged_in()){

        $fname = array();
        $MusicCount = count($_FILES['fname']['name']);
        for ($i = 0; $i < $MusicCount; $i++) {
            $_FILES['file']['name'] = $_FILES['fname']['name'][$i];
            $_FILES['file']['type'] = $_FILES['fname']['type'][$i];
            $_FILES['file']['tmp_name'] = $_FILES['fname']['tmp_name'][$i];
            $_FILES['file']['error'] = $_FILES['fname']['error'][$i];
            $_FILES['file']['size'] = $_FILES['fname']['size'][$i];


            $uploadPath = './uploads/music/';
            $config['upload_path'] = $uploadPath;
            $config['allowed_types'] = 'mp3';

            $this->load->library('upload', $config);
            $this->upload->initialize($config);


            if ($this->upload->do_upload('file')) {

                $musicData = $this->upload->data();
                $uploadMusicData[$i]['fname'] = $musicData['file_name'];
            }
        }
        if (!empty($uploadMusicData)) {
            $title = $this->input->post('title');
            // Insert files data into the database
            $this->m_uploads->save_music($uploadMusicData, $title);
            print_r($uploadMusicData);
        }

        $this->load->view('admin/music_uploaded_successfully');

    }else{
        $this->load->view('admin/login');
    }
}

//这是表格

<form class="form-horizontal" action="<?php echo base_url();?>music_uploaded_successfully" method="post">
<div class="card-body">
    <h4 class="card-title">Upload New Music</h4>
    <div class="form-group row">
        <label for="fname" class="col-sm-3 text-right control-label col-form-label">File</label>
        <div class="col-sm-9">
            <input type="file" name="song" required="required" size="20"/>
        </div>
    </div>

    <div class="form-group row">
        <label for="title" class="col-sm-3 text-right control-label col-form-label">Title</label>
        <div class="col-sm-9">
            <input type="text" name="title" class="form-control" id="title" placeholder="" required="required">
        </div>
    </div>

</div>
<hr>

<div class="card-body">

    <div class="form-group m-b-0 text-right">

        <a href="<?php echo base_url();?>music_json" class="btn btn-success waves-effect waves-light" style="color:#ffffff;">Generate JSON Data</a>

        <button type="submit" class="btn btn-purple waves-effect waves-light">Save</button>
        <button type="submit" class="btn btn-outline-purple waves-effect waves-light">Cancel</button>

    </div>

</div>
</form>

我希望将用户选择的文件上载到文件夹,并将文件名上载到数据库以及其他形式的输入上载到数据库

1 个答案:

答案 0 :(得分:0)

在您的view代码中,文件输入名称为歌曲。但是在控制器中,您正在使用fname。在$this->upload->do_upload()中使用file

请确保您(在视图和控制器上)都使用<input type='file'> name 属性值(根据您的代码,song

使用此

$_FILES['song']['name'][$i]
$this->upload->do_upload('song')

代替

$_FILES['fname']['name'][$i]
$this->upload->do_upload('file')

此外,您还必须在enctype="multipart/form-data"标签中使用form

public function music_upload_success(){
if($this->logged_in()){

    $fname = array();
    $MusicCount = count($_FILES['song']['name']);
    for ($i = 0; $i < $MusicCount; $i++) {
        $_FILES['file']['name'] = $_FILES['song']['name'][$i];
        $_FILES['file']['type'] = $_FILES['song']['type'][$i];
        $_FILES['file']['tmp_name'] = $_FILES['song']['tmp_name'][$i];
        $_FILES['file']['error'] = $_FILES['song']['error'][$i];
        $_FILES['file']['size'] = $_FILES['song']['size'][$i];


        $uploadPath = './uploads/music/';
        $config['upload_path'] = $uploadPath;
        $config['allowed_types'] = 'mp3';

        $this->load->library('upload', $config);
        $this->upload->initialize($config);


        if ($this->upload->do_upload('song')) {

            $musicData = $this->upload->data();
            $uploadMusicData[$i]['song'] = $musicData['file_name'];
        }
    }
    if (!empty($uploadMusicData)) {
        $title = $this->input->post('title');
        // Insert files data into the database
        $this->m_uploads->save_music($uploadMusicData, $title);
        print_r($uploadMusicData);
    }

    $this->load->view('admin/music_uploaded_successfully');

}else{
    $this->load->view('admin/login');
}
}