2016-09-23 70 views
0

控制器:如何上传图像笨

public function edit($id) { 
    $this->edit_status_check($id); 
    $this->form_validation->set_rules('agent_name', 'Agent Name', 'required'); 
    $this->form_validation->set_rules('mobile', 'Mobile No.', 'required'); 
    $this->form_validation->set_rules('agent_vehicle', 'Agent Vehicle', 'required'); 
    if ($this->form_validation->run() == FALSE) { 
     $data = array(
      'page_title' => 'Edit Agent', 
      'page_name' => 'agent/edit', 
      'result' => $this->agent_model->select_id($id), 
      'result_vehicle' => $this->vehicle_model->list_all(), 
      'error' => validation_errors(), 
      'id' => $id 
     ); 
     $this->load->view('template', $data); 
    } else { 
     $config['upload_path'] = '../uploads/agent/'; 
     $config['allowed_types'] = 'jpg|jpeg'; 
     $config['encrypt_name'] = TRUE; 
     $config['max_size'] = 1000; // 1 mb 
     $this->load->library('upload', $config); 
     if (!$this->upload->do_upload('agent_image')) { 
      $data = array(
       'page_title' => 'Edit Agent', 
       'page_name' => 'agent/edit', 
       'result' => $this->agent_model->select_id($id), 
       'result_vehicle' => $this->vehicle_model->list_all(), 
       'error' => $this->upload->display_errors(), 
       'id' => $id 
      ); 
      $this->load->view('template', $data); 
     } else { 
      $_POST['agent_img_url'] = 'uploads/agent/' . $this->upload->data('file_name'); 
      $this->agent_model->update($_POST, $id); 
      alert('Update', $_POST['agent_name']); 
      redirect('agent'); 
     } 
    } 
} 

型号:

public function update($data, $id) { 
    $updatedata = array(
     'name' => $data['agent_name'], 
     'mobile' => $data['mobile'], 
     'password' => sha1($data['password']), 
     'vehicle' => $data['agent_vehicle'], 
     'address' => $data['agent_address'], 
     'category' => $data['category'], 
     'created_on' => date('Y-m-d h:i:sa') 
    ); 
    if (!empty($data['agent_img_url'])) { 
     $updatedata['img_url'] = $data['agent_img_url']; 
    } 
    $this->db->where('id', $id); 
    $this->db->update('agent', $updatedata); 
} 

查看:

<?= form_open_multipart('agent/edit/' . $id); ?> 
    <?php if (!empty($error)): ?> 
     <div class="alert alert-danger alert-dismissible" role="alert"> 
      <?= $error; ?> 
     </div> 
    <?php endif; ?> 
    <div class="form-group"> 
     <img src="/<?= $result['img_url']; ?>" class="img-responsive" name="old_agent_image" width="133" height="100"> 
    </div> 
    <div class="form-group"> 
     <label>Agent Image</label> 
     <input type="file" name="agent_image"> 
    </div> 
    <button type="submit" class="btn btn-success">Update</button> 
<?= form_close(); ?> 

您好我正在开发一个图像uploa d模块和图像路径保存在数据库中并进行检索。 我的问题我想要编辑和更新,但我的问题是它不会删除文件夹中的旧图像,但它会保存并更新新图像。

回答

1

笨的使用文件助手

$this->load->helper("file"); 
delete_files($path); 

参考链接,你是here

+0

C:\ Apache24 \ htdocs \ abcd \ uploads \ agent \ blck.jpg如何在php –

+0

中转换文件路径,将文件路径设置为“uploads \ agent \ blck.jpg”。 –

+0

以上编辑图像代码不能正常工作.. –

1

删除使用保存在数据库中的文件名,使用PHP unlink(../filename.jpg),并从文件中删除