Reputation: 1859
i am using codeigniter mvc framework, i want to make a form that has a upload picture and also save inputted data in input types in one html <form>
why is this my image cannot be uploaded and it displays error like this when i echo the error:
File cannot be uploadedarray(1) { ["error"]=> string(43) "
You did not select a file to upload.
" }
Here is my code in view :
<form id="frm_product" class="form-horizontal" method="POST" action="Admin_controls/addProduct">
<div style="position:relative;">
<a class='btn btn-primary' href='javascript:;'>
Choose Picture..
<input type="file" style='position:absolute;z-index:2;top:0;left:0;filter: alpha(opacity=0);-ms-filter:"progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";opacity:0;background-color:transparent;color:transparent;' name="file_source" size="40" onchange='$("#upload-file-info").html($(this).val());'>
</a>
<span class='label label-info' id="upload-file-info"></span>
</div>
<br>
<div class="form-group">
<label for="cat_name">Product Name: </label>
<input type="text" class="form-control" name="prod_name">
</div>
<div class="form-group">
<label for="cat_desc">Description: </label>
<input type="text" class="form-control" name="prod_desc">
</div>
<div class="form-group">
<label for="cat_desc">Price: </label>
<input type="text" class="form-control" name="prod_price">
</div>
<div class="form-group">
<label for="cat_desc">Category: </label>
<select class="form-control" name="prod_cat">
<?php foreach($data as $each){ ?>
<option value="<?php echo $each->Category_id; ?>"><?php echo $each->Category_name; ?></option>';
<?php } ?>
</select>
</div>
</form>
Here is my javascript to submit the form:
function submitProduct() {
document.getElementById("frm_product").submit();
}
In my controller here is my code:
public function addProduct(){
$config['upload_path'] = './assets/uploaded_images/';
$config['allowed_types'] = 'jpg|jpeg|png';
$config['max_size'] = 1024 * 8;
$this->load->library('upload', $config);
if($this->upload->do_upload("file_source")) {
$upload_data = $this->upload->data();
$file_name = base_url().'assets/uploaded_images/'.$upload_data['file_name'];
$data = array (
'Product_name' => $this->input->post('prod_name'),
'Product_desc' => $this->input->post('prod_desc'),
'Product_price' => $this->input->post('prod_price'),
'Category_id' => $this->input->post('prod_cat'),
'Product_img' => $file_name
);
var_dump($data);
}
else {
echo "File cannot be uploaded";
$error = array('error' => $this->upload->display_errors()); var_dump($error);
}
}
Upvotes: 0
Views: 1182
Reputation: 133
Seems to me that you forgot to add enctype="multipart/form-data"
to your form
<form id="frm_product" class="form-horizontal" method="POST" action="Admin_controls/addProduct" enctype="multipart/form-data">
Upvotes: 1