2017-02-16 101 views
1

我被困在更新圖像的問題上。我創建的圖片上傳工作正常,但我也希望它被更新。當我添加一個需要的圖像時,它會正確更新,但如果我不想更改圖像並保持原樣,那麼我的當前圖像無法恢復。請幫我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>          
+0

你可以添加查看頁面 – Vimal

+0

**是** @ vimal s – anu

+0

你正在將文件移動到一個文件夾..?右..我沒有得到上傳代碼.. – Vimal

回答

0
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'] ?>"> 

嘗試this..let我知道這個作品或沒有。

+0

編輯..更新您的更新功能.. – Vimal

+0

謝謝你vimal..its工作.. – anu

0

總是小心,當您上傳圖像與相同的名稱。它已成功上傳,但您無法看到它的前端已更改爲具有相同的網址和相同的名稱,這是因爲您的瀏覽器具有緩存的上一張圖片。

確保圖像在服務器第一