【问题标题】:File uploaded to destination but name not saved in db in codeigniter文件上传到目的地但名称未保存在 codeigniter 的数据库中
【发布时间】:2018-11-26 09:03:32
【问题描述】:

我正在尝试将收件人姓名和他的照片上传到数据库中。图片已成功上传到上传文件夹中,但未上传到数据库中。这是我的代码。

我的 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";
   }

上传_model.php 类 Upload_model 扩展 CI_Model{

    public function insert_form($tablename , $data){

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

【问题讨论】:

    标签: php codeigniter codeigniter-3


    【解决方案1】:

    希望对您有所帮助:

    上传文件后使用这个

    $this->upload->data('file_name');  
    

    而不是这个:

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

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

    注意:上传成功时在$data赋值($this-&gt;upload-&gt;do_upload('userfile')之后)

    $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

    【讨论】:

      猜你喜欢
      • 2012-10-07
      • 2021-07-15
      • 1970-01-01
      • 2023-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多