我尝试更改控制器中的更新功能, 还添加了删除模型中的旧图片功能。
My update function in controller:
public function update_product() {
$id = $this->input->post('product_id');
$this->load->library('image_lib');
// remove old images
$this->Product_model->remove_old_images($id);
if(!$this->update->do_update('userfile')){
$error = array('error'=>$this->update->display_errors());
$this->load->view('product_form', $error);
}else{
//Main image
$data = $this->upload->data();
$config['image_library'] = 'gd2';
$config['source_image'] = './upload/'.$data["raw_name"].$data['file_ext'];
$config['new_image'] = './upload/'.'new_'.$data["raw_name"].$data['file_ext'];
$config['create_thumb'] = FALSE;
$config['maintain_ratio'] = FALSE;
$config['width'] = 547;
$config['height'] = 430;
}
// make thumb
$configThumb['image_library'] = 'gd2';
$configThumb['source_image'] = './upload/'.$dataThumb["raw_name"].$dataThumb['file_ext'];
//$config2['create_thumb'] = TRUE;
$configThumb['new_image'] = './upload/'.'thumb_'.$dataThumb["raw_name"].$dataThumb['file_ext'];
$configThumb['maintain_ratio'] = TRUE;
$configThumb['width'] = 120;
$configThumb['height'] = 112;
$this->load->library('image_lib', $configThumb);
if (!$this->image_lib->resize()) {
$this->session->set_flashdata('error', $this->image_lib->display_errors());
redirect('/Product/cadeaupagina_aangeboden');
}
$this->image_lib->initialize($configThumb);
$this->image_lib->resize();
$data = array(
'product_naam' => $this->input->post('product_naam'),
'category_id' => $this->input->post('category_id'),
'ophaal_plaats' => $this->input->post('ophaal_plaats'),
'product_beschrijving' => $this->input->post('product_beschrijving'),
'product_foto' => 'new_'.$data["raw_name"].$data['file_ext'],
'product_foto_thumb' => 'thumb_'.$dataThumb["raw_name"].$dataThumb['file_ext'],
);
$data['img'] = base_url().
'/upload/new_'.$data["raw_name"].$data['file_ext'];
$dataThumb['img'] = base_url().
'/upload/thumb_'.$dataThumb["raw_name"].$dataThumb['file_ext'];
$this->Product_model->update($id, $data, $dataThumb);
$this->load->view('cadeaubewerken',$data);
redirect('/Product/cadeaupagina_aangeboden');
}
My model update and remove image functions:
public function get($id = '') {
if (!empty($id)) {
$this->db->where('product_id', $id);
}
return $this->db->get_where('products');
}
public function update($id, $data, $dataThumb) {
return $this->db->update('products', $data, array('product_id' => $id));
}
public function remove_old_images($id = null) {
$i = 0; // num of delete files
if ($id !== null && $this->get($id)->num_rows() == 1) {
$fotos = $this->get($id)->row();
$main = $fotos->product_foto;
$thumb = $fotos->product_foto_thumb;
if (is_file($this->up_path . $main)) {
unlink($this->up_path . $main);
$i++;
}
if (is_file($this->up_path . $thumb)) {
unlink($this->up_path . $thumb);
$i++;
}
return $i;
} else {
return $i;
}
}
我的观看文件格式:
<tr>
<h4>Cadeau naam</h4>
<td><?php echo form_input(array('id'=>'product_naam', 'name'=>'product_naam', 'value' => $product["product_naam"] , 'size'=>25));?></td>
<td><?php echo form_input(array('type'=>'hidden','id'=>'product_id', 'name'=>'product_id', 'value' => $product['product_id']));?>
</tr>
<tr>
<td><?php echo form_open_multipart('Product/upload'); ?> </td>
</tr>
<tr><td>
<h4>Kies een categorie</h4>
<select name="category_id">
<?php foreach (get_categories_h() as $category) :
if ($category->id == $product["category_id"]){
echo '<option value="'.$category->id .'" selected>' . $category->name . '</option>';
}else{
echo '<option value="'.$category->id .'">'.$category->name . '</option>';
}
endforeach; ?>
</select>
</td>
</tr>
<tr>
<td><h4>Ophaal plaats</h4><?php echo form_input(array('id'=>'ophaal_plaats', 'name'=>'ophaal_plaats', 'value' => $product["ophaal_plaats"], 'size'=>25));?></td>
<td><?php echo form_input(array('type'=>'hidden','id'=>'ophaal_plaats', 'name'=>'ophaal_plaats', 'value' => $product['ophaal_plaats']));?>
</tr>
<tr>
<td>
<h4>Gebruik adres van mijn account</h4>
</td>
</tr>
<tr>
<td>
<h4>Huidig cadeau profiel foto</h4>
<img src="<?php echo base_url(); ?>upload/<?php echo $product['product_foto_thumb']; ?>" class="img-responsive">
</td>
</tr>
<tr>
<div class="image-upload">
<label for="file-input">
<td><?php echo form_input(array('type'=>'hidden','id'=>'file', 'name'=>'product_foto', 'value' => $product['product_foto']));?>
<h4>Cadeau profiel foto veranderen</h4>
<input type="file" id="file1" name="userfile"/>
</label>
</div>
<tr>
<td><?php echo form_textarea(array('type'=>'textarea','id'=>'product_beschrijving', 'name'=>'product_beschrijving', 'value' =>$product['product_beschrijving'], 'size'=>25));?></td>
<td><?php echo form_input(array('type'=>'hidden','id'=>'product_beschrijving', 'name'=>'product_beschrijving', 'value' => $product['product_beschrijving']));?>
</tr>
<tr>
<td><input type="submit" class="btn btn-primary" name="submit" value="Cadeau bewerken!" /></td>
</tr>
</table>
</form>
当我提交表单时,我收到了以下错误:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: KdGwcontroller::$update
Filename: controllers/KdGwController.php
Line Number: 42
Backtrace:
File: /home/ubuntu/workspace/application/controllers/KdGwController.php
Line: 42
Function: _error_handler
File: /home/ubuntu/workspace/index.php
Line: 315
Function: require_once
致命错误:在第42行的/home/ubuntu/workspace/application/controllers/KdGwController.php中的非对象上调用成员函数do_update()调用堆栈:0.0004 244440 1. {main}()/ home / ubuntu / workspace / index.php:0 0.0005 247232 2. require_once('/ home / ubuntu / workspace / system / core / CodeIgniter.php')/ home /ubuntu /workspace / index.php:315 0.0051 928288 3。 call_user_func_array:{/ home / ubuntu / workspace / system / core / CodeIgniter.php:532}()/home/ubuntu/workspace/system/core/CodeIgniter.php:532 0.0051 929096 4. KdGwcontroller-&gt; update_product()/家用/ Ubuntu的/工作区/系统/核心/ CodeIgniter.php:532 遇到PHP错误
Severity: Error
Message: Call to a member function do_update() on a non-object
Filename: controllers/KdGwController.php
Line Number: 42
Backtrace:
答案 0 :(得分:0)
基本上你做的事情和你最初上传的时候一样,但是你使用更新查询并从数据库中获取后删除旧文件:
public function update_product() {
$id = $this->input->post('product_id');
// check if user with current id matches user who created the post
// we dont want *smart* users to edit others posts
if ($this->Product_model->get($id)->row()->user_id !== $_SESSION['user_id']) {
show_error('Not authorized to edit this product.'); // this exits
}
if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {
$this->load->library('upload');
$this->load->library('image_lib');
// remove old images
$this->Product_model->remove_old_images($id);
// upload new image
$config['upload_path'] = './upload/';
$config['allowed_types'] = 'gif|jpg|png|bmp|jpeg';
$config['encrypt_name'] = true;
$this->upload->initialize($config);
if (!$this->upload->do_upload('userfile')) {
// handle errors here somehow
//$error = array('error' => $this->upload->display_errors());
//$this->load->view('product_form', $error);
echo $this->upload->display_errors(); // just for debug now
} else {
$upData = $this->upload->data();
// set names
$mainImage = 'new_' . $upData["raw_name"] . $upData['file_ext'];
$thumbImage = 'thumb_' . $upData["raw_name"] . $upData['file_ext'];
// Main image
$configMain['image_library'] = 'gd2';
$configMain['source_image'] = './upload/' . $upData["raw_name"] . $upData['file_ext'];
$configMain['new_image'] = './upload/' . $mainImage;
$configMain['create_thumb'] = FALSE;
$configMain['maintain_ratio'] = FALSE;
$configMain['width'] = 547;
$configMain['height'] = 430;
// Thumnail image
$configThumb['image_library'] = 'gd2';
$configThumb['source_image'] = './upload/' . $upData["raw_name"] . $upData['file_ext'];
$configThumb['new_image'] = './upload/' . $thumbImage;
$configThumb['create_thumb'] = TRUE;
$configThumb['maintain_ratio'] = FALSE;
$configThumb['width'] = 120;
$configThumb['height'] = 112;
$this->image_lib->initialize($configMain);
$this->image_lib->resize();
$this->image_lib->initialize($configThumb);
$this->image_lib->resize();
// delete original
@unlink($upData['full_path']);
// we add filesnames to db $data array
$data['product_foto'] = $mainImage;
$data['product_foto_thumb'] = $thumbImage;
}
}
$data = array(
'product_naam' => $this->input->post('product_naam'),
'category_id' => !empty($this->input->post('category_id')) ? $this->input->post('category_id') : 0,
'ophaal_plaats' => $this->input->post('ophaal_plaats'),
'product_beschrijving' => $this->input->post('product_beschrijving'),
'date_updated' => date('Y-m-d')
);
$this->Product_model->update($id, $data);
echo 'Done'; // handle success however you want... just for debug now
}