CodeIgniter Forums
Update Image - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Development (https://forum.codeigniter.com/forumdisplay.php?fid=6)
+--- Forum: CodeIgniter 3.x (https://forum.codeigniter.com/forumdisplay.php?fid=17)
+--- Thread: Update Image (/showthread.php?tid=80477)



Update Image - pou_to_ksereis - 11-06-2021

Hey friend i cannot update the user image. it overwrites the old image.
please any hint regarding this problem?
thanks Smile
PHP Code:
public function edit($id=""){
$this->rbac->check_operation_access(); // Check auth

        if($this->input->post('submit')){
                $this->form_validation->set_rules('name''Name''trim|required|min_length[5]');
                $this->form_validation->set_rules('surname''Surname''trim|required');
                
                
if ($this->form_validation->run() == FALSE) {
                    $data = array(
                        'errors' => validation_errors()
                    );
                    $this->session->set_flashdata('errors'$data['errors']);
                    redirect(base_url('users/edit/'$id['user_id']));

                } else {

                    //$image = null;

                    if ($_FILES['image_thumb']['name']){

                        $config['upload_path']          './uploads/user_image/';
                        $config['allowed_types']        'gif|jpg|png|jpeg|JPEG|GIF|JPG|PNG';
                        $config['max_size']            "1024";
                        $config['max_width']            "*";
                        $config['max_height']          "*";
                        $config['encrypt_name']        TRUE;
                        $this->upload->initialize($config);
                        $this->load->library('upload'$config);
                        if ( ! $this->upload->do_upload('image_thumb')){

                            $this->session->set_userdata(array('errors'=> $this->upload->display_errors()));
                            redirect(base_url('users/edit/'.$id['user_id']));
                        
                        
} else {
                            $image =$this->upload->data();
                            $image_url "uploads/user_image/".$image['file_name'];

                            //delete old image
                            $old_image $this->input->post('old_image');
                            $old_file_thumb =  substr($old_imagestrrpos($old_image'/') + 1);
                            @unlink(FCPATH 'uploads/user_image/' $old_file_thumb);

                        }
                    }

                    $old_image $this->input->post('old_image');

                    $data = array(
                        'image'        => (!empty($image_url) ? $image_url $old_image), 



RE: Update Image - kenjis - 11-13-2021

> i cannot update the user image. it overwrites the old image.

If the old image is overwritten, it seems you can update the user image.
I don't get what you say correctly.

See https://codeigniter.com/userguide3/libraries/file_uploading.html
and try a simple code first.


RE: Update Image - InsiteFX - 11-13-2021

If your change an image and want to create an updated image then you first need to save the orginal image.


RE: Update Image - pou_to_ksereis - 11-15-2021

(11-13-2021, 01:44 AM)InsiteFX Wrote: If your change an image and want to create an updated image then you first need to save the orginal image.

Hello
The firrst one is saved on db, and is loaded in the value, the overwrite of the image doesnt work

<div class="mt-3">
                                <label class="form-label">Image</label>
                                <input class="form-control" name="image_thumb" id="image_thumb" type="file">
                                <img alt="img pic" class="rounded-md" src="<?php echo base_url() . $row['image']; ?>">
                                <input class="form-control" name="old_image" id="old_image" type="hidden" value="<?php echo $row['image'];?>">
        </div>



RE: Update Image - InsiteFX - 11-15-2021

Are you using form open and form close? Without those it will never return anything to the controller post data.


RE: Update Image - pou_to_ksereis - 11-15-2021

(11-15-2021, 02:33 AM)InsiteFX Wrote: Are you using form open and form close? Without those it will never return anything to the controller post data.

Yes sure :

        <?php echo form_open(base_url('product/edit/'.$row['id']), 'class="form-horizontal"');  ?>

       //Code for edit product

      <?php echo form_close(); ?>



RE: Update Image - kenjis - 11-19-2021

https://codeigniter.com/userguide3/libraries/file_uploading.html#preferences

The default value of overwrite is false.


RE: Update Image - pou_to_ksereis - 11-23-2021

Still no result,
I added $config['overwrite']    = false; as @kenjis say but still the first image is displayed even after the replace Sad
if ($_FILES['image_thumb']['name']) {
                        $config['upload_path'] = './uploads/product_image/';
                        $config['allowed_types'] = 'gif|jpg|png|jpeg|JPEG|GIF|JPG|PNG';
                        $config['max_size'] = "1024";
                        $config['max_width'] = "*";
                        $config['max_height'] = "*";
                        $config['encrypt_name'] = TRUE;
                        $config['overwrite']    = false;