Welcome Guest, Not a member yet? Register   Sign In
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;
                }

            }

}

}
#2

[eluser]Ed Robindon[/eluser]
I believe the file name is in the $_FILES array as is the extent.
#3

[eluser]Unknown[/eluser]
//create array to load to database
$insert_data = array(
'name' => $image_data['file_name'],
'path' => $image_data['full_path'],
'thumb_path'=> $image_data['file_path'] . 'thumbs/'. $image_data['file_name'],
'tag' => $tag
);

$this->db->insert('photos', $insert_data);//load array to database

=====================
Hi i am copy this code for you from
http://stackoverflow.com/questions/36302...upload-lib
And i hope it will be help for you, just get file name using $image_data['file_name'] and create an array then put it in this insert function $this->db->insert('photos', $insert_data)
=====================
#4

[eluser]johnpeace[/eluser]
It's kind of poor etiquette to code-bomb the forums with your entire application's code. Is that really necessary? I mean, do I need to see your whole giant mess to just tell you to get the filename for the uploaded file from the File Upload data() method?

http://ellislab.com/codeigniter/user-gui...ading.html




Theme © iAndrew 2016 - Forum software by © MyBB