C

Simplified tutorials to learn the popular basic language C

VB.Net

Learn the basic desktop development application language

CodeIgniter

One of the popular PHP three tier (MVC) framework

How to get Uploaded File Name and Insert into the Database


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.

View(fileupload.php):

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

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

     if($this->upload->do_upload('myfile'))
     {
          $filedata = $this->upload->data();
          $filename = $filedata['raw_name'].$filedata['file_ext'];
          $username = $this->input->post('username');
          $this->load->Model('ProjectModel');
          $this->ProjectModel->addnewfile($filename,$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

$this->load->Model('ProjectModel');               
$this->ProjectModel->addnewfile($filename);
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)
  {
    $this->load->database();
    $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.

No comments:

Post a Comment