Press "Enter" to skip to content

Getting Uploaded File Name and Insert into the Database in CodeIgniter

In our previous lesson, we successfully uploaded files into the project directory. There we just uploaded files into the project image folder, but we do not keep any details of files on the database.

The methodology of file uploading is lies with uploading the files on the project directory and database keep the details of the files such as name, formats, size, uploading date, etc.
While retrieving the uploaded files back to the project, database gives the details of the required files and PHP code finds the appropriated file from the uploaded file directory. These details are used for determining the file path.

In our previous lesson, we uploaded some files, here let us see how to insert file details like file name, file extension and uploaded user name into our MySQL database.


<title>File Upload</title>
<body bgcolor="#b1ac4e">
<div class="maindiv">
 <?php echo form_open_multipart(base_url().'user/uploading') ?>
	<td><b>Name </b> </td><td><input type="text" name="username"/></td>
	<td><b>Image</b> </td><td> <input type="file" name="myfile" size="20" /></td>
	<td>  </td><td> <input type="submit" value="Register" style="height: 25px; width: 100px; float: center;"/></td>

Action sends submitted data into the controller function ‘uploading’:

Controller (uploading):

public function uploading()
     $config['upload_path'] = './images';
     $config['allowed_types'] = 'gif|jpg|png|jpeg';
     $config['max_size']	= '2000';
     $config['max_width']  = '20000';
     $config['max_height']  = '30000';
     $this->load->library('upload', $config);

$filedata = $this->upload->data();
$filename = $filedata[‘raw_name’].$filedata[‘file_ext’];
$username = $this->input->post(‘username’);

Until the code $this->upload->do_upload(‘myfile’)), that we discussed in our previous lesson.
Here, if statements works well, if and only if the file is uploaded to the specified directory.

$filedata = $this->upload->data();
Here variable $filedata get the data(details) of the uploaded file.

$filename = $filedata[‘raw_name’].$filedata[‘file_ext’];
Here $filedata[‘raw_name’] is the uploaded file name and $filedata[‘file_ext’] is the file extension(format). Both the name and extension are concatenated with ‘.’ operation and saved into the variable ‘$filename’.

For example:
If you upload an image file named with ‘myphoto.JPG’, then variable $filedata[‘raw_name’] get the value ‘myphoto’ and variable $filedata[‘file_ext’] gets the value ‘.JPG’. And the variable $filename gets the total value, that is ‘myphoto.JPG’.
If you upload same file again, then the filename will be renamed.

$username = $this->input->post(‘username’);
Here $username gets a value of input box ‘username’.

Upload image in CodeIgniter into database

Here model ‘ProjectModel’ is loaded and $this->ProjectModel->addnewfile($filename,$username); calls a model function ‘addnewfile’ with parameters $filename and $username.

Model (addnewfile):

  public function addnewfile($filename,$username)
    $data = array(
         'user_name' => $username,
         'user_pic' =>$filename

 $this->db->insert(‘table_user’, $data);
As we leant in a previous lesson, filename and user name are inserted into the database. Where ‘user_pic’ and ‘user_name’ are the column names on the database table ‘table_user’.

Inserting file name into the mysql database in CodeIgniter

Displaying uploaded image from database:

Now we successfully inserted images into the project folder ‘images’ and their names are inserted into the database.
Now let us see how to get back and show the file (here image) on a page.
First of all, we should select (retrieve) file name from database, which is based on the user name that we inserted with file uploading.

Here if you give a user name, then corresponding image will be displayed. That we will learn in next lesson.

Be First to Comment

Leave a Reply