1

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']    
));  
2

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

have you checked my answer?
sir, can you also help for how to view image in browser from database like other fields of table i tried image name is display, but how can display image
for that you have to upload image in one of the folders of your project and to show you have to put the path of image in src attribute of image
1

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.

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.