Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8505627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:22:28+00:00 2026-06-11T02:22:28+00:00

I have a small app in codeigniter. There is a form that has 3

  • 0

I have a small app in codeigniter. There is a form that has 3 text fields, name,email,location. This is enough to submit. However there are two optional fields to all the user to submit either an image or video or both. I have it working until there is an error with the file uploaded. It will still submit the text data, when i want it to halt the whole submission until the error is fixed.

Here is the code from my controller. Any help would be great. Thanks!

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Form extends MY_Controller {

public function __construct()
{
    parent::__construct();
    $this->load->helper(array('form', 'url'));
}

function index()
    {
        $this->load->helper(array('form', 'url'));

        $this->load->library('form_validation');

        $this->form_validation->set_rules('nominee', 'Nominee', 'required');
        $this->form_validation->set_rules('email', 'Email', 'required|valid_email');
        $this->form_validation->set_rules('location', 'Location', 'required');

        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('form');
        }
        else
        {
             // Load the library - no config specified here
                $this->load->library('upload');

                // Check if there was a file uploaded - there are other ways to
                // check this such as checking the 'error' for the file - if error
                // is 0, you are good to code
                if (!empty($_FILES['imageupload']['name']))
                {
                    // Specify configuration for File 1
                    $config['upload_path'] = 'uploads/images';
                    $config['allowed_types'] = 'gif|jpg|png';
                    $config['max_size'] = '100';
                    $config['max_width']  = '1024';
                    $config['max_height']  = '768';       

                    // Initialize config for File 1
                    $this->upload->initialize($config);

                    // Upload file 1
                    if ($this->upload->do_upload('imageupload'))
                    {
                        $data = $this->upload->data();
                    }
                    else
                    {
                        echo $this->upload->display_errors();
                    }

                }

                // Do we have a second file?
                if (!empty($_FILES['videoupload']['name']))
                {
                    // Config for File 2 - can be completely different to file 1's config
                    // or if you want to stick with config for file 1, do nothing!
                    $config['upload_path'] = 'uploads/videos/';
                    $config['allowed_types'] = 'gif|jpg|png';
                    $config['max_size'] = '100';
                    $config['max_width']  = '1024';
                    $config['max_height']  = '768';

                    // Initialize the new config
                    $this->upload->initialize($config);

                    // Upload the second file
                    if ($this->upload->do_upload('videoupload'))
                    {
                        $data = $this->upload->data();
                    }
                    else
                    {
                        echo $this->upload->display_errors();
                    }

                }

            $this->load->view('thankyou');
            //Run code to add into the database
        }
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T02:22:30+00:00Added an answer on June 11, 2026 at 2:22 am

    Place all the upload data logic into a custom validation callback.

    Then if the validation fails (including the uploads), the page will return to the form, with the data pre-filled in – and your user can fix the file upload.

    Then your controller logic is simplied as well;

        $this->load->library('form_validation');
    
        $this->form_validation->set_rules('nominee', 'Nominee', 'required');
        $this->form_validation->set_rules('email', 'Email', 'required|valid_email');
        $this->form_validation->set_rules('location', 'Location', 'required');
        $this->form_validation->set_rules('imageupload', 'Image Upload', 'callback__image_upload');
        $this->form_validation->set_rules('videoupload', 'Video Upload', 'callback__video_upload');
    
        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('form');
        }
        else
        {
            //Run code to add into the database
            $this->load->view('thankyou');
        }
    

    and

    function _video_upload()
    {
       //put upload logic here and check if its valid
    }
    
    function _image_upload()
    {
       //put upload logic here and check if its valid
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a small app that has a Render thread. All this thread does
I have a small app that from the home screen has a few buttons,
I have this small app that runs on any phone having API level 6
I have this small app that loads plugin type components that other users can
I have a small app where the index page is a form that instructs
I have a small web app I wrote that has a Dijit.layout.BorderContainer nested within
i have written a small codeigniter test app that is currently running on my
I have a small utility app written in Visual Basic 6 that has been
I have small web app that generate PDF files as a report. I'm trying
I have a small app that downloads some files from a remote (HTTP) server

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.