CodeIgniter Forums
$this->upload->data() into a different function? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: $this->upload->data() into a different function? (/thread-54461.html)



$this->upload->data() into a different function? - El Forum - 09-10-2012

[eluser]Unknown[/eluser]
Hi,

I have the following controller for a form that allows an optional video and image upload but the first half of the form (text fields) is required. This all works brilliant except one thing. I need to be able to store the filename of the image and video into the database in the form1() function.

The _imageupload and _videoupload are callbacks in the validation so, my question is how can i return the data i need from $this->upload->data() ?

Any help would be great.

Thanks in advance.


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

/*----------------------------------------------------------------
  Form 1
----------------------------------------------------------------*/

public function form1($imgname = null) {

  //Perform Validation
  $this->load->library('form_validation');

     $this->form_validation->set_rules('nominee', 'Nominee', 'required');
     $this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique[self_nominees.email]');
     $this->form_validation->set_rules('location', 'Location', 'required');
     $this->form_validation->set_rules('category', 'Category', 'required');
     $this->form_validation->set_rules('description', 'Description', '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)
     {
   //Load the form again.
   $this->template('form.php');
     }
     else
     {  
   $imgfilename = 'test';
   $videofilename = 'test2';
  
         //Run code to add into the database
   $formdata = $this->input->post(NULL, TRUE); // this prevent from XSS attacks
   $this->load->model('users');
   $this->users->self_nominate($formdata['nominee'], $formdata['email'], $formdata['location'], $formdata['category'], $formdata['description'], $imgfilename, $videofilename);        

   //Load the thankyou page
   $this->template('thankyou.php');
     }
  
}


/*----------------------------------------------------------------
  Image Upload Function
----------------------------------------------------------------*/

function _image_upload()
{
    $this->load->library('upload');

            // Check if there was a file uploaded
            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 = array('upload_data' => $this->upload->data());
     return true;
                }
                else
                {
                    $imageerrors = $this->upload->display_errors();
     $this->form_validation->set_message('_image_upload', $imageerrors);
    
     return false;
                }

            }

}

/*----------------------------------------------------------------
  Video Upload Function
----------------------------------------------------------------*/

function _video_upload()
{
    $this->load->library('upload');

            // Check if there was a file uploaded
            if (!empty($_FILES['videoupload']['name']))
            {
                // Specify configuration for File 2
                $config2['upload_path'] = 'uploads/videos';
                $config2['allowed_types'] = 'gif|jpg|png';
                $config2['max_size'] = '100';
                $config2['max_width']  = '1024';
                $config2['max_height']  = '768';      

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

                // Upload file 2
                if ($this->upload->do_upload('videoupload'))
                {
                    $data = $this->upload->data();
     return true;
                }
                else
                {
                    $videoerrors = $this->upload->display_errors();
     $this->form_validation->set_message('_video_upload', $videoerrors);

     return false;
                }

            }

}

}



$this->upload->data() into a different function? - El Forum - 09-10-2012

[eluser]rogierb[/eluser]
the easiest way is to specify 2 vars
Code:
class Form extends MY_Controller {
var $video_name;
var $image_name;
....

and populate them in the callback
Code:
$this->video_name = 'the_video_name';