Welcome Guest, Not a member yet? Register   Sign In
Optional file uploads in form in codeigniter. Please help!
#1

[eluser]Unknown[/eluser]
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!


Code:
<?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
        }
    }

}




Theme © iAndrew 2016 - Forum software by © MyBB