• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can i insert the filename of upload into database?

#1
[eluser]Unknown[/eluser]
Does anybody know how I can get the name of the file that has just been uploaded and then pass it to the index() function so it can be inserted into my database?

It's written in codeigniter and here is the code from my Form Controller.

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

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

public function form1() {

  //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
     {  
    
         //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'], 'image_name', 'video_name');        

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

/*----------------------------------------------------------------
  Form 2
----------------------------------------------------------------*/

public function form2() {
  
  //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('form2.php');
     }
     else
     {  
  
         //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'], 'image_name', 'video_name');        

   //Load the thankyou page
   $this->template('thankyou2.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;
                }

            }

}

}


Messages In This Thread
How can i insert the filename of upload into database? - by El Forum - 09-07-2012, 08:39 AM
How can i insert the filename of upload into database? - by El Forum - 09-07-2012, 09:10 AM
How can i insert the filename of upload into database? - by El Forum - 09-07-2012, 09:44 AM
How can i insert the filename of upload into database? - by El Forum - 09-07-2012, 10:56 AM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.