Codeigniter:Update Image and Display

2019-05-29 07:26发布

问题:

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

Controller

public function insert()
        {
       $data['s_em']=$this->input->post('s_em');
       $data['s_na']=$this->input->post('s_na');
    $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png|jpeg|jpe|pdf|doc|docx|rtf|text|txt';
             $this->load->library('upload', $config);
                                  if ( ! $this->upload->do_upload('file'))
                                      {
                              $error = array('error' => $this->upload->display_errors());

                                      }

                             else
                                 {

                        $data['file']=$this->upload->data('file_name');


                 } 

              $this->Students_m->db_ins($data);


         $this->load->view('admin/newstudents');


     }

    public function edit($id)
     {
        $dt['da']=$this->Students_m->db_edit($id)->row_array();
        $this->load->view('admin/st_edt',$dt);
      }   

public function update()
{
    $id=$this->input->post("id");

    $s_em=$this->input->post("s_em");
    $s_na=$this->input->post("s_na");

    $config['upload_path'] = './uploads/';
    $config['allowed_types'] =     'gif|jpg|png|jpeg|jpe|pdf|doc|docx|rtf|text|txt';
    $this->load->library('upload', $config);
    if ( ! $this->upload->do_upload('file'))
    {
        $error = array('error' => $this->upload-   >display_errors());
    }
    else
    {
        $upload_data=$this->upload->data();
        $image_name=$upload_data['file_name'];
    }

$data=array('s_em'=>$s_em,'s_na'=>$s_na,'file'=>$image_name);
$this->Students_m->db_update($data,$id);
}

redirect("admin/students");
}

Model

 public function db_ins($data)
     {
    $query=$this->db->insert('student',$data);
    return $query;   
    }
public function db_edit($id)
{
    return $this->db->get_where('student', array('id'=>$id));
}
public function db_update($data,$id)
{
    $this->db->where('id', $id);       
    $this->db->update('student', $data);
}

view

  <form action="../update" method="post" enctype="multipart/form-data">

    <legend class="text-semibold">Personal data</legend>
  <img src=" <?php echo base_url( 'uploads/'. $da['file']);?>" height="205" width="205">

                                <div class="row">
                                      <div class="col-md-6">
                                            <div class="form-group">
                                        <label class="display-block">image:<span class="text-danger">*</span></label>
                                        <input name="file" type="file" id="image_id" class="file-styled ">
                                        <span class="help-block"></span>
                                    </div>
                                </div>
                    <div class="col-md-6">
                                    <div class="form-group">
                                        <label>Email address: <span class="text-danger">*</span></label>
                                        <input type="email" name="s_em" class="form-control required"   value="<?php echo $da['s_em'];?>">
                                    </div>
                                </div>
                            </div>
                            <div class="row">

                            <div class="col-md-6">
                                    <div class="form-group">
                                        <label>Student name: <span class="text-danger">*</span></label>
                                        <input type="text" name="s_na" class="form-control required"  value="<?php echo $da['s_na'];?>" id="n1">
                                    </div>
                                </div>
                            <button type="submit">Update<i class="icon-check position-right"></i></button>
                     <input type="hidden" name="id" value="<?php echo $da['id'] ;?>">
                    </form>
                </div>                                    

回答1:

    public function update()
    {
        $id=$this->input->post("id");

        $s_em=$this->input->post("s_em");
        $s_na=$this->input->post("s_na");


 if($_FILES[file]['name']!="")
            {
    $config['upload_path'] = './uploads/';
        $config['allowed_types'] =     'gif|jpg|png|jpeg|jpe|pdf|doc|docx|rtf|text|txt';
        $this->load->library('upload', $config);
        if ( ! $this->upload->do_upload('file'))
        {
            $error = array('error' => $this->upload-   >display_errors());
        }
        else
        {
            $upload_data=$this->upload->data();
            $image_name=$upload_data['file_name'];
        }
    }
    else{
                $image_name=$this->input->post('old');
            }
$data=array('s_em'=>$s_em,'s_na'=>$s_na,'file'=>$image_name);
$this->Students_m->db_update($data,$id);
}

in the view file add the following

<input type="hidden"  id="old"  name="old"  value="<?php echo $da['file']   ?>">

try this..let me know this works or not.



回答2:

Be always careful when you upload image with a same name. It's uploaded successfully but you can't see the if changed on the front end of it has same URL and same name that is because your browser have Cached previous Image.

Make sure image is updated successfully on server first



标签: codeigniter