Codeigniter:Update Image and Display Current image if I don't want to update old image

1.2k views Asked by At

I'm stuck on a problem with updating image. I've created image upload which works fine but I also want it to be updated. When I add a need image it updates correctly but I if don't want to change the image and leave it as it is, then my current image can't be retrieve.

Please help me

Here is my code:

my model update

 function updateDatasale($id){
          
          $data=array(
 
              'mayor_permit_attachment'=>$this->mayors_attachment()
             
              
          );
          $this->db->where('id',$id);
          $this->db->update('tickets',$data);
      }

function mayors_attachment(){

            $pic=array(
                'upload_path'=>'./uploads/',
                'allowed_types'=>'gif|jpg|png',
                'max_size'=>4000,
                'max_width'=>10024,
                'max_height'=>10024,
            );
            $this->load->library("upload",$pic);
            
            if($this->upload->do_upload('mayors_attachment')){
                $fb=$this->upload->data();
                $fd=$fb['file_name'];
                return $fd;
            }
            else{
                $data = "null";
                return $data;
            }

 }

controller

public function updatesales($id){
    $this->TicketReply_model->updateDatasale($id);
    redirect("accepttask");
}

views

<tr>
  <td>
      <span class="text-danger text-bold">
           <label>Mayors Permit:</label>
             <input type="file" name="mayors_attachment" >                           
 </td>  
</tr>
                
2

There are 2 answers

1
Samuel Asor On BEST ANSWER

One way that I've used to solve this kind of problem was to store the file name (or the full path) in a session or cookie. After storing, you can easily retrieve this detail and use it wherever you want.

In your upload file check block:

if($this->upload->do_upload('mayors_attachment')){
// other code here
$attachment_session_data = array('attachment_file' => $fb['full_path']); // or the file_name if your prefer
$this->session->set_userdata($attachment_session_data); // add to session
// return normally
}

Then in your view file, using a value attribute for a file input doesn't count and it is not being submitted to the form. So, a workaround will be to echo an img tag, to show the image, then while processing the form, you would check if a new value is entered in the input file and also if a session already exists the previously entered image.

Something like so:

VIEW FILE

<tr>
  <td>
      <span class="text-danger text-bold">
           <label>Mayors Permit:</label>
             <input type="file" name="mayors_attachment" >   
             <img src="<?php echo $this->session->attachment_file; ?>">
 </td>  
</tr>

Ideally, $this->session->attachment_file should be in your controller and saved as part of the $data array that will be sent to the view, then you can just call or echo the variable in the view file. (I hope you get the point).

Then in your CONTROLLER or MODEL:

// Check if session exists and input file is empty
if ( $this->session->attachment_file && empty($_FILES) )
{
    // No file was entered, no need to upload
}
else 
{
    // New file was entered, do upload.
}
2
KUMAR On

First You have to store hidden image from database with input type file option. and then check in controller function with if else{} image is available or not?

views

<form method="POST" action="<?php echo base_url().'ControllerName/updateImageData/'.$id;?>" enctype="multipart/form-data">  
<tr>
  <td>
      <span class="text-danger text-bold">
           <label>Mayors Permit:</label>

           //store hidden image from database.

           <input type="hidden" name="mayors_attachment_oldimage"  value="<?php echo $mayors_attachment;?>">
           
           <input type="file" name="mayors_attachment">                           
 </td>  
</tr>

<button class="btn btn-primary form-control" type="submit">Update Details</button>

</form>

Controller Code :

 public function updateImageData($aid)
    {       
         if($_FILES['mayors_attachment']['name']!=""){

             $config['upload_path'] = "./assets/uploads/mayors_attachment/";
            $config['allowed_types'] = 'gif|jpeg|png|jpg';
            $this->load->library('upload',$config);
            $this->path = './assets/uploads/mayors_attachment/';

           $this->upload->initialize($config);

            if (!$this->upload->do_upload('mayors_attachment'))
            {
              $error = array('error'=>$this->upload->display_errors());
           }else{
           $imageUpload = $this->upload->data();
           $mayors_attachment_image_name=$imageUpload['file_name'];
            }
        }
         else{
                $mayors_attachment_image_name=$this->input->post('mayors_attachment_oldimage');
             
            }
            
         $insertdata = array(
              'mayors_attachment'=>$mayors_attachment_image_name);  
            
        $insertQuery = $this->db->where('id',$aid)->update('tickets',$insertdata);  
    }