php - 无法上传数据库中的文件
问题描述
我正在向数据库中上传 PDF 文件,但存在一些问题,所有数据都已成功上传,但无法在数据库中上传文件
控制器
function register_candidate(){
if($this->input->post()){
if ( ! $this->upload->do_upload('resume'))
{
$filename = '';
}
else
{
$filename = $this->upload->data('file_name');
}
$dataArray = array('salutation'=>$this->input->post('salutation',true),
'candidate_first_name'=>$this->input->post('candidate_first_name',true),
'candidate_last_name'=>$this->input->post('candidate_last_name',true),
'nationaity'=>$this->input->post('nationaity',true),
'candidate_phone'=>$this->input->post('candidate_phone',true),
'candidate_email'=>$this->input->post('candidate_email',true),
'candidate_job_title'=>$this->input->post('candidate_job_title',true),
'salary'=>$this->input->post('salary',true),
'candidate_password'=>md5($this->input->post('candidate_password', true)),
'resume'=>$filename
);
//print_r($dataArray);
if($this->Candidate_model->register_candidate($dataArray)){
$this->session->set_flashdata('success', 'Data Successfully Inserted');
redirect('Candidate/register_candidate');
}else{
$this->session->set_flashdata('error', 'Failed to insert');
redirect('Candidate/register_candidate');
}
}else{
$this->load->view('frontend/header');
$this->load->view('home/register_user');
$this->load->view('frontend/footer');
}
}
仅作为文件上传数据的问题无法上传,并且在数据库中显示空白数据
解决方案
文件上传类
CodeIgniter 的文件上传类允许上传文件。您可以设置各种首选项,限制文件的类型和大小。
控制器:
public function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|pdf';
$config['max_size'] = 100;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('userfile'))
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}
}
加载上传库:
$this->load->library('upload');
设置首选项:
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|pdf';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
// Alternately you can set preferences by calling the ``initialize()`` method. Useful if you auto-load the class:
$this->upload->initialize($config);
上述偏好应该是不言自明的。下表描述了所有可用的首选项。
推荐阅读
- php - 请求类的可选参数
- laravel - 如何在 php7+ 中使用 CURL 发送多个同名文件
- javascript - 使用javascript添加图像后浏览器滚动条移位
- lambda-calculus - Beta 减少 - Lambda 演算
- db2 - Data too long db2 中 Varchar(600) 类型列的错误(Windows 和 10.0.22 版本)
- wpf - WPF 表单使用 Powershell 获取 O365 客户端访问设置
- validation - 如何在测试我的应用程序时从验证团队获取有关错误的详细信息
- sql-server - 运行 PowerShell 命令以通过 T-SQL (xp_cmdshell) 强制停止进程
- iot - Thingsboard 是否默认将遥测数据存储到数据库中?
- go - 使用工厂模式时应该在哪里定义接口?