user5456337
user5456337

Reputation:

want to change image in codeigniter

I want to change an image in Codeigniter, replace the previously uploaded image and also delete previous image from folder :

Here is my Controller . But its not working:

public function changeImage(){

    //$user_id = $this->profile_model->changeImage();

        /************* File Upload ************/
        $config['upload_path'] = './uploads/user_pic/';
        $config['allowed_types'] = 'gif|jpg|png|jpeg';
        $this->load->library('upload',$config);

        $filetype = $_FILES['user_pic']['type'];
        $file_name = $_FILES['user_pic']['name'];

        if($filetype == "image/jpg")
                $file_type='jpg';
            else if ($filetype == "image/gif")
                $file_type='gif';
            else if($filetype == "image/jpeg")
                $file_type='jpg';

            else if($filetype == "image/pjpeg")
                $file_type='pjpeg';
            else if($filetype ==  "image/png")
                $file_type='png';

        $_FILES['user_pic']['name']=$user_id.'.'.$file_type;

        $this->upload->do_upload('user_pic');


        $up_dtat = array('user_pic' => $_FILES['user_pic']['name']);
        $this->db->where('user_id',$user_id);
        $this->db->update('tbl_users',$up_dtat);
    redirect('profile');
}

Upvotes: 0

Views: 1235

Answers (2)

Nidhi
Nidhi

Reputation: 1539

If you want to replace the previous image only but set file name same then

First method is :

get old file name from table

$sql = "SELECT user_pic FROM tbl_users WHERE user_id=?";
$get_image = $this->db->query($sql,array($user_id))->row_array();

set new file name as assigned to old

$config['file_name'] = $get_image['user_pic'];
$config['overwrite'] = TRUE; // this will overwrite your image
$this->load->library('upload', $config);

But in this case, old file and new file extension must be same If both extensions are different then maybe image will be crashed after upload

The second method is

Get user_pic data from DB

And Unlink that picture

unlink($_SERVER['DOCUMENT_ROOT'].'/'.Your_image_folder.$get_image['user_pic']);

And check file is upload or not

 if ( ! $this->upload->do_upload('user_pic')) {
            $error = array('error' => $this->upload->display_errors()); 
         }

         else { 
            $data = array('upload_data' => $this->upload->data()); 
         } 

Upvotes: 0

Astound
Astound

Reputation: 192

if you need to update the profile picture in your store file, first you need to remove the old file, fetch the old file name on the bases of user id.

Then give the file name with this code.

unlink("your_path/filename.jpg"); //don't use base_url() just give the path like profile_pic/xxxxx.png

This code will delete the specified file from that folder.

Now i have code that i had use for the file uploads so it will also work for you try this.

$imgpath='uploads/profile_pic/';//path where do you want to store image
            $all_img="";
            if($_FILES["profile_pic"]['name'])//input type that you have use in file upload
            {         
               $path_parts = pathinfo($_FILES["profile_pic"]["name"]);
               $image_path = $path_parts['filename'].'_'.time().'.'.$path_parts['extension']; 
               $all_img.=$image_path;
               move_uploaded_file($file_tmp=$_FILES["profile_pic"]["tmp_name"],$imgpath."/".$image_path);
               $data['cm_user_profile_pic']=$all_img;//store filename in array to update in database
            }

Upvotes: 1

Related Questions