文件上传到目标但名称未保存在codeigniter中的db中

时间:2018-06-17 04:57:53

标签: php codeigniter codeigniter-3

我正在尝试在数据库中上传Receivers名称和他的照片。图片成功上传到uploads文件夹中,但不在数据库中。这是我的代码。

我的Html代码

<?php echo form_open_multipart('upload/do_upload');?>

<div class="form-row">
 <div class="form-group col-md-4">
  <label class="label_color" for="inputState">Receiver Name</label>
  <input type="text" class="form-control" id="inputCity" name="receiver_name">
</div>
<div class="form-row">
 <div class="form-group col-md-4 upload-border float-left">
  <label class="label_color" for="inputState">Picture Upload</label>
  <input type="file" name="userfile" size="20">
  <input class="mt-4" type="submit">
</div>

我在controller.php中的代码

public function do_upload(){
    $this->form_validation->set_rules('record_number', 'Record Number', 'required');

    $data= array(
    'record_number' => $this->input->post('record_number'),
    'pic' => $this->input->post('userfile')
   );
        $config = array(
        'upload_path' => './uploads/',
        'allowed_types' => "gif|jpg|jpeg|png|iso|dmg|zip|rar|doc|docx|xls|xlsx|ppt|pptx|csv|ods|odt|odp|pdf|rtf|sxc|sxi|txt|exe|avi|mpeg|mp3|mp4|3gp",
        'max_size' => "2048000", // Can be set to particular file size , here it is 2 MB(2048 Kb)
        'max_height' => "950",
        'max_width' => "1250"
);

文件上传工作正常。而文件名未保存在数据库列中。已在数据库中定义了一列。

$insert = $this->upload_model->insert_form('upload_table', $data);

    if ($insert == true) {
        echo "file Submitted";
    }else{
    echo "file submit failed";
   }

Upload_model.php     class Upload_model扩展了CI_Model {

    public function insert_form($tablename , $data){

    if ($this->db->insert($tablename, $data)) {
        return true;
    }else{
        return false;
    }
}

1 个答案:

答案 0 :(得分:1)

希望这会对您有所帮助:

上传文件后使用此

$this->input->post('userfile')

而不是:

$data

您的$data变量应该是这样的:

注意:在上传成功时($this->upload->do_upload('userfile')之后){/ 1}}中分配值

$data= array(
'record_number' => $this->input->post('record_number'),
'pic' => $this->upload->data('file_name')
);

了解更多:https://www.codeigniter.com/user_guide/libraries/file_uploading.html#CI_Upload::data