Wednesday, 10 February 2016

How to upload images using codeigniter?

How to upload images using CodeIgniter: First create the HTML form in which you will make the field of type "file" and the form have attribute define the "enctype" of HTML form that is multipart/form-data and then the controller part that has the configs and also here we tell you about how to rename the file and save in the database of that file name.

How to upload images using codeigniter

upload picture HTML

<div class="row">
<div class="col-md-6">
<form method="post" action="<?php echo BURL; ?>/UserControl/uploadpicturefile" enctype="multipart/form-data">
<div class="form-group">
<label for="file">User Name</label>
<input type="file" name="file" class="form-control" id="file" placeholder="file">
</div>
<button type="submit" class="btn btn-default">Upload</button> </form>
</div>
</div>


Common Mistake: Here is the HTML part of the image upload that has one special attribute named enctype that is required for uploading the images you should use it otherwise you can not access the image file name.

upload picture login in PHP controller file.
public function uploadpicturefile(){
$id = $this->session->userdata('id');
    $config['upload_path'] = 'assets/user_images/';
$config['allowed_types'] ='*';
$config['max_size'] = '*';
$config['max_width']  = '*';
$config['max_height']  = '*';
$new_name = $id . $_FILES["file"]["name"];
$config['file_name'] = $new_name;
// insert new name of picture in signup table profileImage name
$this->Mod_user->insert_pic_name($id, $new_name);
$this->load->library('upload');
$this->upload->initialize($config);
if (!$this->upload->do_upload('file'))
{
$status = 'error';
                       $msg = $this->upload->display_errors('<p>', '</p>');
                      echo $msg;
}else{
$data = array('file_uploaded' => $this->upload->data());
print_r($data);
echo 'else';
redirect(BURL. '/UserControl/userprofile');
}
 }
How to upload images using codeigniter

I hope you know about the CodeIgniter controller that has the function to upload the image file. We explain the code written above is first of all

  • Set the upload path.
  • Allow the types of files that you want to upload.
  • Set the size of an image file.
  • Set the name of image file.
  • Load the library of upload that is the Codeigniter library.
  • Upload the file.

These are simple steps that you need to follow and the image file name and other parameters insert into the database and the image file is saved in the folder that you set the path. You can further read about the database connection using CodeIgniter.
How to create connection with database in CodeIgniter...!

This Post Was Last Updated On September 30, 2016, By Author: Mahira Khan.

0 comments:

Post a Comment

Item Reviewed: How to upload images using codeigniter? Rating: 5 Reviewed By: admin