我想通过引导模式上传文件。文件未上传 我已经尝试了其他几种方法,我只是得到了相同的结果。我应该能够上传 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">×</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;
}
请帮助我哪里出了问题
你忘了添加一行,请添加此行添加,然后尝试。
$_FILES['upload_file'] = $_FILES['file'];
$config['file_name'] = $_FILES['upload_file']['name'];
如果没有,请尝试从此链接。
https://codeigniter.com/userguide3/libraries/file_uploading.html
您应该更改上传到文件夹的路径,如下所示 -
$config['upload_path'] = './uploads';
或者您可以使用 -
$config['upload_path'] = realpath('uploads');
这是关于您的问题的类似问题。
如果您正在自动加载library
,也可能发生此错误,因此初始化它可能会解决您的问题(参考(。
$this->load->library('upload', $config);
$this->upload->initialize($config);
看看它是否对你有帮助。
像这样更改上传路径。
$config['upload_path'] = './uploads/';
不要忘记最后一个/. 看看它是否有帮助