tags:

views:

96

answers:

2

Hi friends, It's ok to upload image in file using codeigniter upload library with this example.

However, I want to send filename in database. Can anybody pls give me example ? Thanks

<?php

class Upload extends Controller {

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

function index()
{   
    $this->load->view('upload_form', array('error' => ' ' ));
}

function do_upload()
{
    $config['upload_path'] = './uploads/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '100';
    $config['max_width']  = '1024';
    $config['max_height']  = '768';

    $this->load->library('upload', $config);

    if ( ! $this->upload->do_upload())
    {
        $error = array('error' => $this->upload->display_errors());

        $this->load->view('upload_form', $error);
    }   
    else
    {
        $data = array('upload_data' => $this->upload->data());

        $this->load->view('upload_success', $data);
    }
}   
}
?>
A: 

You can get it with data() method like this:

$upload_data = $this->upload->data();
echo $upload_data['file_name'];

Note that you can also get other upload data, just check what you have:

print_r($upload_data);
Sarfraz
do i need to write model function ? like send_file_info_to_db() and write $this->db->insert('some_table') in controller ? any code help would be great. Thanks
sagarmatha
you don't have to use your model, but it is a good practice to do so...
rabidmachine9
I did $this->db->insert('upload_file', $data['upload_data']);but it's sending all the data arrays, I just want four of those fields going to database.How do i do that ? please help
sagarmatha
A: 
rabidmachine9
Thank you very much, this is very close to what i want , howerver I am getting this error: Message: Undefined variable: image_data and Column 'file_name' cannot be nullINSERT INTO `upload_file` (`file_name`, `full_path`, `orig_name`) VALUES (NULL, NULL, NULL) ......... my show create table is CREATE TABLE `upload_file` ( `id` int(11) NOT NULL, `file_name` varchar(80) NOT NULL, `full_path` varchar(80) NOT NULL, `orig_name` varchar(80) NOT NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1
sagarmatha
try and put this line of code before:$image_data = $this->upload->data();
rabidmachine9