首页 > 解决方案 > 无法上传数据库中的文件

问题描述

我正在向数据库中上传 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');

        }
    }

仅作为文件上传数据的问题无法上传,并且在数据库中显示空白数据

标签: phpcodeigniter

解决方案


文件上传类

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);

上述偏好应该是不言自明的。下表描述了所有可用的首选项。


推荐阅读