Codeigniter:更新图像和显示

时间:2017-02-16 06:43:40

标签: codeigniter

我遇到了更新图片的问题。我已经创建了图片上传工作正常,但我也希望它能够更新。当我添加一个需要的图像时,它会正确更新,但如果我不想更改图像并保持原样,那么我的当前图像无法检索。请帮帮我

控制器

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");
}

模型

 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);
}

视图

  <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>                                    

2 个答案:

答案 0 :(得分:2)

    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);
}

在视图文件中添加以下内容

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

尝试这个..我知道这是否有效。

答案 1 :(得分:0)

上传具有相同名称的图片时务必小心。它已成功上传,但您无法看到它前端的更改是否具有相同的URL和相同的名称,因为您的浏览器已缓存以前的图像。

确保首先在服务器上成功更新图像