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>
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:
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 toecho
animg
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
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: