I'm trying to make an update form so users are able to change a product picture. The form works fine everything gets updated except for the picture.
Also I have a product_foto_thumb and a normal product_foto so I don't know how to do that in my view.
This is how I have it in my view:
<input type="file" name="userfile"/>
<td>
<?php echo form_input(array('type'=>'hidden','id'=>'product_foto', 'name'=>'product_foto', 'value' => $product['product_foto']));?>
</td>
But I guess I also need an input type for the product_foto_thumb?
And this is my update controller function:
public function update_product()
{
$id= $this->input->post('product_id');
$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_thumb' => 'thumb_'.$dataThumb["raw_name"].$dataThumb['file_ext'],
'product_foto' => 'new_'.$data["raw_name"].$data['file_ext'],
);
$this->Update_product_model->update_product_function($id,$data);
}
So the form works but except when I select a new picture and I submit the form, the old picture is removed and there is no new picture on the product. Can anyone help me?
Try this code, definitely will help you.
if($this->input->post('submit')){
if(($_FILES['userfile'])){
$errors= array();
$file_name = $_FILES['userfile']['name'];
$file_size = $_FILES['userfile']['size'];
$file_tmp = $_FILES['userfile']['tmp_name'];
$file_type = $_FILES['userfile']['type'];
$ext = pathinfo($file_name, PATHINFO_EXTENSION);
$expensions= array("jpeg","jpg","png");
// this is used for unique file name always
$mtime = uniqid(microtime());
$uniqueid = substr($mtime,2,8);
$filename = $uniqueid.'.'.$ext;
if(in_array($ext,$expensions)=== false){
$errors[]="extension not allowed, please choose a JPEG or PNG file.";
}
if($file_size > 2097152) {
$errors[]='File size must be excately 2 MB';
}
if(empty($errors)==true) {
move_uploaded_file($file_tmp,"upload_folder_name_here/".$filename);
}
}
$id= $this->input->post('product_id');
$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_thumb' => 'thumb_'.$filename,
'product_foto' => 'new_'.$filename,
);
$this->Update_product_model->update_product_function($id,$data);
}