4

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

Or do we have any alternative?

4 Answers 4

4

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);
Sign up to request clarification or add additional context in comments.

3 Comments

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
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
I know this is 4 years ago, but it really helped me out, Thank you!!
2

Create an array with whatever you need to upload in your db...and then insert it in the db put something like this under else:

 //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 

Important:the indexes of the array got to have same name with the columns of your table!

also try and use your model it will make your code a bit easier to maintain,read etc best of luck!

2 Comments

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 null INSERT 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
try and put this line of code before: $image_data = $this->upload->data();
0

you can use this way :

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

Comments

0
$config['upload_path'] = './assets/uploads/';  // Upload Path will be here
        $config['allowed_types'] = 'gif|jpg|png';
        $this->upload->initialize($config);

        if ($this->upload->do_upload('upload')) {
           $file_data = $this->upload->data();
           $file_name= $file_data['file_name']; //get file name of your uploaded file from here..

         }
         else { 

          echo "Error";
         } 

        $data = array(

        'image_name' =>  $file_data['file_name'], //insert your image name from here..
        );

        $this->add_model->image_upload($data); //that will go to the model function..
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.