How to insert file name into database using lib

In this example, you can upload an image to a file using the codeigniter download library.

However, I want to send the file name to the database. Can someone give me an 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 an alternative?

+4
source share
4 answers

Create an array with what you need to load into your db ... and then paste it into db, put something like this in 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: array indexes get the same name as the columns of your table!

also try and use your model, this will make your code a little easier to maintain, read, etc. good luck!

+2
source

You can get it using the data() method as follows:

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

Please note that you can also get other data to download, just check that you have:

 print_r($upload_data); 
+4
source

you can use this method:

 $data = array('upload' => $this->upload->data()); echo $data['upload']['file_name']; 
0
source
 $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.. } 
0
source

All Articles