首页 > 解决方案 > 文件不会在 CodeIgniter 中上传

问题描述

我想通过 bootstrap modal 上传文件。文件没有上传我已经尝试了其他几种方法我得到了相同的结果。我应该能够上传 pdf 和图像

以下是我的控制器

 public function add_file() {

    $config['upload_path'] = '/emp/uploads';
    $config['allowed_types'] = 'jpg|jpeg|png|pdf';
    $config['max_size'] = 2048000;
    $config['max_width'] = 1600;
    $config['max_height'] = 1600;
    $config['encrypt_name'] = TRUE;
    $this->load->library('upload', $config);
    if (!$this->upload->do_upload('file')) {
        redirect('employees/display');
    } else {
        $data['emp_id'] = $this->input->post('emp');
        $data ['type'] = $this->input->post('type');
        $data ['file_name'] = $this->upload->data('file_name');
        $this->employee_model->insert_f($data);
    }
}

看法:

  <form action="<?php echo base_url('employees/add_file'); ?>" method="post" enctype="multipart/form-data">
                <div class="modal fade" id="newmodal" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
                    <div class="modal-dialog" role="document">
                        <div class="modal-content">
                            <div class="modal-header">
                                <h5 class="modal-title" id="exampleModalLabel">Upload</h5>
                                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                    <span aria-hidden="true">&times;</span>
                                </button>
                            </div>
                            <div class="modal-body">
                                <div class="form-group">
                                    <label class="col-sm-2 col-form-label">File Type</label>

                                    <input type="text" name="type" class="form-control" >
                                </div>

                                <div class="form-group">
                                    <label class="col-sm-2 col-form-label">File</label>

                                    <input type="file" name="file" size="20" class="form-control" >

                                </div>


                                <input type="hidden" name="emp"  class="form-control" value ="<?php echo$emp->emp_id ?>">
                                <input type="hidden" name="file_name"  class="form-control" >

                                <div class="modal-footer">
                                    <button type="button" class="btn btn-secondary btn-sm" data-dismiss="modal">Close</button>
                                    <button type="submit" class="btn btn-success btn-sm" name="submit"> Save</button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

            </form>

模型

 public function insert_f($data){
    $this->db->insert('emp_doc',$data);
  $id = $this->db->inser_id();
  return $id;
}

请帮我看看哪里出了问题

标签: codeigniterbootstrap-modal

解决方案


您应该更改上传文件夹的路径,如下所示 -

$config['upload_path'] = './uploads';

或者你可以使用 -

$config['upload_path'] = realpath('uploads'); 

是关于您的问题的类似问题。

如果您正在自动加载,也会发生此错误library,因此初始化它可能会解决您的问题(参考)。

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

看看对你有没有帮助。


推荐阅读