You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将CodeIgniter中PDF上传大小从2MB提升至5MB以上

Got it, let's fix that 2MB upload limit for your PDF files in CodeIgniter. There are two key parts you need to adjust: the CodeIgniter upload configuration AND your PHP server settings (since PHP has its own default limits that can override framework settings). Here's how to do it step by step:

Step 1: Update CodeIgniter's Upload Configuration

Add a max_size parameter to your upload config array. This sets the maximum file size the framework will accept, measured in kilobytes (5MB = 5120KB). If you need even larger files, just increase this number (e.g., 10240 for 10MB).

Step 2: Adjust PHP Server Limits

PHP has global upload limits that will block files even if CodeIgniter allows them. You'll need to modify these in your php.ini file (or use .htaccess/user.ini if you don't have server access):

  • Set upload_max_filesize = 5M (or higher)
  • Set post_max_size = 6M (this should always be larger than upload_max_filesize to account for other form data)
    Restart your web server (Apache/Nginx) after making these changes. If using .htaccess, add these lines instead:
php_value upload_max_filesize 5M
php_value post_max_size 6M

Modified Full Code

I also cleaned up a couple of syntax issues in your original code (like extra space in a database key and messy else blocks) and added error handling for upload failures:

public function question_upload() {
    $this->form_validation->set_rules('examname', 'Select Exam Type', 'required');
    $this->form_validation->set_rules('assignment', 'Subject Name', 'required');
    $this->form_validation->set_rules('question', 'Content Name', 'required');
    $this->form_validation->set_rules('subcontent', 'Sub Content Name', 'required');

    if (empty($_FILES['picture']['name'])) {
        $this->form_validation->set_rules('picture', 'Pdf', 'required');
    }

    if ($this->form_validation->run() == FALSE) {
        $data['h'] = $this->Faculty_Model->faculty_QuestionDisplay();
        $data['studentAnswer'] = $this->Faculty_Model->student_Question();
        $this->load->view('index1', $data);
    } else {
        $picture = '';
        if (!empty($_FILES['picture']['name'])) {
            $config['upload_path'] = 'uploads/files/';
            $config['allowed_types'] = 'jpg|jpeg|png|gif|pdf';
            $config['file_name'] = $_FILES['picture']['name'];
            // Set max file size to 5MB (5120 KB)
            $config['max_size'] = 5120;

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

            if ($this->upload->do_upload('picture')) {
                $uploadData = $this->upload->data();
                $picture = $uploadData['file_name'];
            } else {
                // Show upload error to the user
                $this->session->set_flashdata('error_msg', $this->upload->display_errors());
                redirect('welcome/index1');
            }
        }

        $userData = array(
            'datee' => $this->input->post('datee'),
            'examname' => $this->input->post('examname'),
            'assignment_name' => $this->input->post('assignment'),
            'question' => $this->input->post('question'), // Fixed extra space in key name
            'subcontent' => $this->input->post('subcontent'),
            'pdffile' => $picture
        );

        $this->Faculty_Model->faculty_Questioninsert($userData);
        $this->session->set_flashdata('success_msg', 'User data have been added successfully.');
        redirect('welcome/index1');
    }
}

Quick Notes

  • If you need to allow files larger than 5MB, just update the max_size value (multiply MB by 1024 to get KB) and adjust the PHP limits accordingly.
  • The added error handling will show users exactly why their upload failed (e.g., "The file you are attempting to upload is larger than the permitted size.").

内容的提问来源于stack exchange,提问作者Swathi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:57:31