vivekanand gangesh
vivekanand gangesh

Reputation: 99

how to insert image in database using codeigniter

How to insert image in database without uploading any folder?

My controller side code is below :

$data['f_name'] = $this->input->post('f_name');  
$data['l_name'] = $this->input->post('l_name');  
$data['contact'] = $this->input->post('contact');  
$data['email'] = $this->input->post('email');  
$data['uid'] = $this->input->post('uid');  
//$data['user_image'] = $this->input->post->userfiles['file_name'];  

My model side code id below :

$this->db->where('id',$data['uid']);  
$this->db->update('users',array(  
'first_name' =>     $data['f_name'],  
'last_name' =>    $val['l_name'],  
'email'  =>    $val['email'],  
 'phone'  =>    $data['contact']    
));  

Upvotes: 1

Views: 6727

Answers (2)

Pradeep
Pradeep

Reputation: 9707

Hope this will help you :

Use $_FILES to get file name

First your form should be like this :

<form action="<?=site_url('controller_name/method_name');?>" enctype="multipart/form-data" method="post" accept-charset="utf-8">

  <input type="file" name="test" id="">

  /*and other input fields*/

  <button type="submit" name="submit">submit</button>
  </form>

In controller get the file name using variable $_FILES

public function method_name()
{
   // as you mention u don't want to upload the file

   $file_name = $_FILES['test']['name'];
   $data['user_image'] = $file_name; 

   $data['f_name'] = $this->input->post('f_name');  
   $data['l_name'] = $this->input->post('l_name');  
   $data['contact'] = $this->input->post('contact');  
   $data['email'] = $this->input->post('email');  
   $data['uid'] = $this->input->post('uid');


}

For more : http://php.net/manual/en/reserved.variables.files.php

Upvotes: 1

David
David

Reputation: 6084

An image is always first uploaded to a temporary folder. It's common that afterwards it shall be stored in a folder, it's then moved with the PHP-function move_uploaded_file($filename, $destination) to a folder of choice. You can read about the common procedures and pitfalls on php.net.

If you want to store the file(s) in the database I suppose you could do it from the temporary directory, but cleaning up that directory you had to do by yourself. Important to know is the Variable $_FILES which holds an array including details like temporary-name(s) and upload-name(s).

Be advised that it might be hard to handle the database for moving, duplicating, etc. especially in the case of any error if many files are stored inside. The database can get very fast very large. It's not a fault to store images in the database but it's not common practice and has some negative aspects. At least I'd separate media then from any data in the database by separated tables, so your decision should have some impact in the database-design.

Upvotes: 1

Related Questions