First of all sorry for the long post with code, but i feel like i need to explain myself straight. The issue is, i'm trying to insert some data into a database and also the file name in the RESTAURANT_IMAGE column. Everything inserts perfectly fine but the image doesn't upload and the file name is not inserted into the database.
EDIT: Edited the callback function to properly return True or False and now i do the insert in the main function if the callback returns true.
Now i am able to upload the image successfully but still dont get the file name and extension to put into my database.
function restaurant_add()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$config = array(
array(
'field' => 'RestaurantName',
'label' => 'Restaurant Name',
'rules' => 'required|callback_restaurant_check'
),
array(
'field' => 'RestaurantAddress',
'label' => 'Address',
'rules' => 'required'
),
array(
'field' => 'RestaurantReservations',
'label' => 'Reservations',
'rules' => 'max_length[1]|integer'
),
array(
'field' => 'RestaurantWifi',
'label' => 'RestaurantWifi',
'rules' => 'max_length[1]|integer'
),
array(
'field' => 'RestaurantDelivery',
'label' => 'Restaurant Delivery',
'rules' => 'max_length[1]|integer'
),
array(
'field' => 'RestaurantMultibanco',
'label' => 'RestaurantMultibanco',
'rules' => 'max_length[1]|integer'
),
array(
'field' => 'RestaurantImage',
'label' => 'RestaurantImage',
'rules' => ''
)
);
$this->form_validation->set_rules($config);
if ($this->form_validation->run() == FALSE)
{
$this->load->view('sample_navbar_view');
$this->load->view('restaurant_add');
return FALSE;
}
else
{
$file_name = $this->do_upload();
$data = array(
'RESTAURANT_NAME' => $this->input->post('RestaurantName'),
'RESTAURANT_ADDRESS' => $this->input-> post('RestaurantAddress'),
'RESTAURANT_RESERVATIONS'=> $this->input-> post('RestaurantReservations'),
'RESTAURANT_WIFI'=> $this->input-> post('RestaurantWifi'),
'RESTAURANT_DELIVERY'=> $this->input-> post('RestaurantDelivery'),
'RESTAURANT_MULTIBANCO'=> $this->input-> post('RestaurantMultibanco'),
'RESTAURANT_OUTDOOR_SEATING'=> $this->input-> post('RestaurantOutdoorSeating'),
'RESTAURANT_IMAGE'=> $file_name
);
$this->load->model('restaurant_model');
$this->restaurant_model->restaurant_add($data);
return TRUE;
}
}
Callback function to check if the restaurant name exists, if it does, it doesn't register it, if it doesn't sends the data into the model.
function restaurant_check($restaurantname){
$this->load->model('restaurant_model');
$result = $this->restaurant_model->check_restaurant_name($restaurantname);
if($result > 0) {
return FALSE;
}
else {
return TRUE;
}
}
I used the upload file article present in the Codeigniter Documentation.
View Code:
<div class="container">
<div class="row">
<div class="col-md-4 col-md-offset-4">
<?php echo form_open_multipart('restaurant/restaurant_add');?>
<form name="restaurant_add" method="post" action="<?php echo site_url().'Restaurant/restaurant_add'; ?>">
<h2>Register your Restaurant</h2>
<input class="form-control" type="text" class="user" name="RestaurantName" placeholder="Restaurant Name">
<input class="form-control"type="text" class="pass" name="RestaurantAddress" placeholder="Restaurant Address">
<input class="form-control"type="number" class="numero" name="RestaurantReservations" placeholder="Restaurant Rerservations">
<input class="form-control"type="number" class="numero" name="RestaurantWifi" placeholder="Wi-Fi">
<input class="form-control"type="number" class="numero" name="RestaurantDelivery" placeholder="Home Deliveries">
<input class="form-control"type="number" class="numero" name="RestaurantMultibanco" placeholder="Have Multibanco?">
<input class="form-control"type="number" class="numero" name="RestaurantOutdoorSeating" placeholder="Esplanada">
<input class="form-control"type="file" class="image" name="RestaurantImage" placeholder="Upload a Picture">
<div class="container">
<?php echo validation_errors(); ?>
</div>
<button class="btn btn-danger"type="submit">Registar</button>
</form>
</div>
</div>
EDIT: Model Code:
function restaurant_add($info)
{
$query =
"INSERT INTO RESTAURANTS
(RESTAURANT_NAME,RESTAURANT_ADDRESS,RESTAURANT_RESERVATIONS,
RESTAURANT_WIFI,RESTAURANT_DELIVERY,RESTAURANT_MULTIBANCO,
RESTAURANT_OUTDOOR_SEATING,RESTAURANT_IMAGE
)
VALUES
(
'".$info['RESTAURANT_NAME']."',
'".$info['RESTAURANT_ADDRESS']."',
'".$info['RESTAURANT_RESERVATIONS']."',
'".$info['RESTAURANT_WIFI']."',
'".$info['RESTAURANT_DELIVERY']."',
'".$info['RESTAURANT_MULTIBANCO']."',
'".$info['RESTAURANT_OUTDOOR_SEATING']."',
'".$info['RESTAURANT_IMAGE']."'
)
";
$result = $this->db->query($query);
return TRUE;
}
function check_restaurant_name($restaurantname) {
$this->db->trans_begin();
$query = "SELECT RESTAURANT_NAME FROM RESTAURANTS WHERE RESTAURANT_NAME = '".$restaurantname."'";
$result = $this->db->query($query);
$rows = $result->num_rows();
if ($this->db->trans_status() === FALSE)
{
$this->db->trans_rollback();
}
else
{
$this->db->trans_commit();
}
return $rows;
}
do_upload() function:
public function do_upload()
{
$config['upload_path'] = './assets/images/restaurantes';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 2000;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('userfile'))
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('success_view', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}
$upload_data = $this->upload->data();
//Returns array of containing all of the data related to the file you uploaded.
$file_name = $this->upload->file_name;
return $file_name;
}
Related
As per question title i can't manage to insert checkbox data into database
Here is my model
public function rules() {
return array(
array( 'field' => '_butype_',
'label' => 'Business Type',
'rules' => 'required|trim|alpha_numeric_spaces|min_length[2]|max_length[50]'),
array( 'field' => '_bumotiv_[]',
'label' => 'Motivation',
'rules' => 'required'),
);
}
public function getById($id){
return $this->db->get_where($this->table, array($this->primary => $id) )->row_array();
}
public function getAll() {
return $this->db->get($this->table)->result_array();
}
public function insert(){
$post = $this->input->post();
if (!empty($post)){
$data = array(
'ginfo_id' => NULL,
'ginfo_butype' => htmlspecialchars($post['_butype_']),
'ginfo_bumotiv' => json_encode($$post['_bumotiv_[]']),
);
$data = $this->security->xss_clean($data);
if($this->db->insert($this->table, $data)){
$response = array(
'status' => 'success',
'message' => 'Success insert data',
);
} else {
$response = array(
'status' => 'error',
'message' => 'Failed insert data',
);
}
} else {
$response = array(
'status' => 'error',
'message' => 'Data not found!',
);
}
return $response;
}
Here is my Controller
public function add(){
$this->form_validation->set_rules($this->M_Ginfo->rules());
if ($this->form_validation->run() === TRUE) {
$this->session->set_flashdata('notif', $this->M_Ginfo->insert());
redirect(site_url('admin/ginfo/add'),'refresh');
} else {
$data['notif'] = $this->M_Auth->notification();
$this->load->view('admin/ginfo/add.php', $data);
}
}
Here is my model
<div class="form-group row">
<label for="_bumotives_[]" class="col-sm-3 col-form-label" style="text-align: left;">What actually motivated you to start your own business? </label>
<div class="col-sm-6">
<input class="form-group" type="checkbox" name="_bumotiv_[]" id="_bumotiv_[]" value="To be independent from working for other people" <?php echo set_checkbox('bumotiv_[]', 'To be independent from working for other people', false); ?>>
<label for="_bumotiv_[]" >To be independent from working for other people. </label>
<br>
<input class="form-group" type="checkbox" name="_bumotiv_[]" id="_bumotiv_[]" value="I have a bright idea that can be commercialised" <?php echo set_checkbox('bumotiv_[]', 'I have a bright idea that can be commercialised', false); ?>>
<label for="_bumotiv_[]" >I have a bright idea that can be commercialised.</label>
<br>
</div>
There are other data that got insert successfully only checkbox is the only form field that has not been submitted to the database
I tried many solution but non worked for me
Any help is appreciated
If non of the checkbox is selected you will not get any result for checkbox.
Check your code for insert query there is an extra $
'ginfo_bumotiv' => json_encode($$post['_bumotiv_[]']),
I have modified the code to use a foreach loop, now I can access the files names, and the print_r is printing the files names in an array like this:
Array ( [0] => Uconn.png [1] => UW_Medicine.png [2] => Yale.png ) Axis.png
but I am still getting the following error:
A PHP Error was encountered
Severity: Notice
Message: Array to string conversion
and a databse error:
Error Number: 1054
Unknown column 'Array' in 'field list'
INSERT INTO `yacht_slider` (`yacht_slide_id`, `slide`) VALUES (87, Array)
I just don't know how should I pass the looped files inside the model to upload.
I am trying to upload a featured image in one input, and multiple images in another input at the same time, I tried many ways and methods to do this, but I always end with an error, mostly Array to string conversion error.
the featured image is stored in one database table and the multiple images are stored in another table.
My current code:
HTML:
<div class="form-group">
<label for="" class="control-label">Image</label>
<input type="file" class="form-control" name="featured">
</div>
<div class="form-group">
<label for="" class="control-label">Slider</label>
<input type="file" class="form-control" name="userfile[]" multiple>
</div>
Model:
public function create_yacht($yacht_img, $slider){
$slug = url_title($this->input->post('title'));
$data = array(
'title' => $this->input->post('title'),
'slug' => $slug,
'img' => $yacht_img,
'city' => $this->input->post('city'),
'category' => $this->input->post('category'),
'price' => $this->input->post('price'),
'description' => $this->input->post('description')
);
$this->db->insert('yachts', $data);
$insert_id = $this->db->insert_id();
$data_4 = array(
'yacht_slide_id' => $insert_id,
'slide' => $slider
);
$this->db->insert('yacht_slider', $data_4);
}
Controller:
public function create_yacht(){
$data['title'] = 'Create';
$data['categories'] = $this->category_model->get_categories();
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('city', 'City', 'required');
if($this->form_validation->run() === FALSE){
$this->load->view('templates/admin_header');
$this->load->view('admin/create_yacht', $data);
$this->load->view('templates/admin_footer');
}else{
$config['upload_path'] = './assets/images';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '2048';
$this->load->library('upload', $config);
$featured = 'featured';
if (!$this->upload->do_upload($featured)) {
$errors = array('errors' => $this->upload->display_errors());
$yacht_img = 'https://via.placeholder.com/1920x1080';
}else{
$data = array('upload_data' => $this->upload->data());
$yacht_img = $_FILES['featured']['name'];
}
foreach ($_FILES['userfile']['name'] as $name) {
$this->upload->initialize($config);
$this->upload->do_upload($name);
$data = array('upload_data' => $this->upload->data());
$slider = $_FILES['userfile']['name'];
}
print_r($slider);
print_r($yacht_img);
$this->yacht_model->create_yacht($yacht_img, $slider);
// redirect('admin');
}
}
I've cleaned up your code a bit and put in some error reporting so users won't be befuddled when an error occurs.
Controller:
public function create_yacht() {
$data['title'] = 'Create';
$data['categories'] = $this->category_model->get_categories();
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('city', 'City', 'required');
if ($this->form_validation->run() === FALSE) {
$this->load->view('templates/admin_header');
$this->load->view('admin/create_yacht', $data);
$this->load->view('templates/admin_footer');
} else {
$this->load->library('upload');
$upload_path = './testupload/';
// just in case, make path if it doesn't exist
// if we can't die
if (!is_dir($upload_path) && #mkdir($upload_path, DIR_WRITE_MODE) == false) {
show_error('Could not make path!');
}
$config['upload_path'] = $upload_path;
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '2048';
$config['file_ext_tolower'] = true;
//$config['encrypt_name'] = true; // might be a good idea
$this->upload->initialize($config);
// featured image
if (!$this->upload->do_upload('featured')) {
show_error($this->upload->display_errors());
}
$yacht_img = $this->upload->data('file_name');
// multi images
$slider_images = array();
$multi_files = $_FILES['userfile'];
if (!empty($multi_files['name'][0])) { // if slider images are required remove this if
$multi_count = count($_FILES['userfile']['name']);
for ($i = 0; $i < $multi_count; $i++) {
$_FILES['userfile']['name'] = $multi_files['name'][$i];
$_FILES['userfile']['type'] = $multi_files['type'][$i];
$_FILES['userfile']['tmp_name'] = $multi_files['tmp_name'][$i];
$_FILES['userfile']['error'] = $multi_files['error'][$i];
$_FILES['userfile']['size'] = $multi_files['size'][$i];
if (!$this->upload->do_upload()) {
// failure cleanup to prevent orphans
#unlink($upload_path . $yacht_img);
if (count($slider_images) > 0) {
foreach ($slider_images as $image) {
#unlink($upload_path . $image);
}
}
show_error($this->upload->display_errors());
}
$slider_images[] = $this->upload->data('file_name');
}
}
$this->yacht_model->create_yacht($yacht_img, $slider_images);
}
}
Model:
public function create_yacht($yacht_img, $slider_images) {
$slug = url_title($this->input->post('title'));
$data = array(
'title' => $this->input->post('title'),
'slug' => $slug,
'img' => $yacht_img,
'city' => $this->input->post('city'),
'category' => $this->input->post('category'),
'price' => $this->input->post('price'),
'description' => $this->input->post('description')
);
$this->db->insert('yachts', $data);
$insert_id = $this->db->insert_id();
if (count($slider_images) > 0) {
foreach ($slider_images as $image) {
$this->db->insert('yacht_slider', array('yacht_slide_id' => $insert_id, 'slide' => $image));
}
}
}
if you are using mySQL 7 then it supports json datatype and you can save as array. If your using lower version of MySQL best solution is to convert your image array into JSON format then insert that into table.
For more details on JSON in MySQL you can refer
https://dev.mysql.com/doc/refman/8.0/en/json.html#json-values
I don't know where I got wrong but I couldn't find it out.
I have a form validation with a callback file check validation function
My Add_Post Function:
public function add_post()
{
$validation = array (
array(
'field' => 'post_title',
'label' => 'Post title',
'rules' => 'trim|required|alpha_numeric'
),
array(
'field' => 'headerimage',
'rules' => 'required|callback_file_check'
),
array(
'field' => 'post_desc',
'label' => 'Post Description',
'rules' => 'trim|required|alpha_numeric'
),
array(
'field' => 'post_content',
'label' => 'Post content',
'rules' => 'trim|required'
)
);
$this->form_validation->set_rules($validation);
if($this->form_validation->run()===FALSE)
{
$info['errors'] = validation_errors();
$info['success'] = false;
}
else
{
$config = array(
"upload_path" => "./uploads/blog_images",
"max_size" => "2048000",
"allowed_types"=> "gif|png|jpg|jpeg",
"file_name" => 'header_'.substr(md5(rand()),0,7),
"overwrite" => false
);
$this->load->library('upload', $config);
if($this->upload->do_upload('file'))
{
$uploadFile = $this->upload->upload_data();
$uploadFileName = $uploadFile['file_name'];
$data = array(
"post_title" => $this->input->post('post_title'),
"post_content" => $this->input->post('post_content'),
"post_image" => $uploadFileName,
"post_created" => $this->input->post('time')
);
$this->Blog_model->add_post($data);
}
$info['success'] = true;
$info['message'] = "Successfully added blog post";
}
$this->output->set_content_type('application/json')->set_output(json_encode($info));
}
Callback function:
public function file_check($str){
$allowed_mime_type_arr = array('application/pdf', 'image/gif', 'image/jpeg', 'image/pjpeg', 'image/png', 'image/x-png');
$mime = get_mime_by_extension($_FILES['headerimage']['name']);
if(isset($_FILES['headerimage']['name']) && $_FILES['headerimage']['name'] != "") {
if(in_array($mime, $allowed_mime_type_arr)) {
return true;
} else {
$this->form_validation->set_message('file_check', 'Please select only pdf/gif/jpg/png file.');
return false;
}
} else {
$this->form_validation->set_message('file_check', 'Please choose a file to upload.');
return false;
}
}
Here's for the view part:
<?php echo form_open_multipart('Blog/file_check',array("class"=>"form-horizontal","id"=>"blogform")); ?>
<div class="row">
<div class="col-md-6">
<input type="text" placeholder="Enter your post title" name="post_title" class="form-control">
</div>
<div class="col-md-6 form-group">
<label for="file" class="control-label col-md-4">Select header image:</label>
<div class="col-md-8">
<input type="file" name="headerimage" id="file" accept="image/*" />
</div>
</div>
</div>
<input type="text" placeholder="Enter your post description" name="post_desc" class="form-control">
<label class="control-label text-muted">Post Body:</label>
<div>
<textarea id="post_content" name="content"></textarea>
</div>
<button class="btn btn-default pull-right" type="button" id="save-post"><i class="fa fa-save"></i> Save Post</button>
<div class="clearfix"></div>
<?php echo form_close(); ?>
My Ajax code:
$(document).on('click','#save-post',function(){
$post_content = $('#post_content').summernote('code');
$time = $('#time').text();
$.ajax({
url:site_url('Blog/add_post'),
data: $('#blogform').serialize() + "&post_content=" + $post_content + "&time=" + $time,
type: "POST",
dataType: 'json',
encode: true,
success: function(data){
if(!data.success) {
if(data.errors) {
$('#blog-message').html(data.errors).addClass('alert alert-danger');
}
} else {
alert(data.message);
}
}
});
});
Input FileName Checked: I already checked the input name and it's the same.
Image Size: I checked for some related question of mine which is undefined index on file validation and they said image size must be big but I use a small size image sample.
I used Krajee's File Input Plugin.
controller:
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');
}
}
}
Model:
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);
}
View:
<?= 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(); ?>
Hi I'm developing a image upload module and image path save in database and retrieve.
my Question I want it to edit and update but the my problem is it doesn't delete the old image in folder, but it save and update the new image.
use file helper of codeigniter
$this->load->helper("file");
delete_files($path);
reference link for you is here
Delete using the file name saved in the database, use the PHP unlink(../filename.jpg) and delete from files
Change in Model
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['agent_img_url'] = $data['agent_img_url'];
}
$q = $this->db->where('id',$id)
->get('agent');
$query = $q->row_array();
#unlink("./asset/uploads/".$query['agent_img_url']);
$this->db->where('id', $id);
$this->db->update('agent', $updatedata);
}
if (!$this->upload->do_upload($name)) {
$data = array('msg' => $this->upload->display_errors());
} else {
$data = array('msg' => "success");
$databasea['upload_data'] = $this->upload->data();
$this->load->library('image_lib');
return $databasea['upload_data']['file_name'];
}
return '';
I have a form which asks for user's name and description and an optional field of image uploading. When the user uploads the image I want the name of the image to be changed and stored into the database and if no image is uploaded then a default name is stored in the database. It works fine if user uploads an image but if he does not then nothing is being uploaded into the database.
This is the controller function :
function store()
{
$this->load->model('campus_m');
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['file_name'] = preg_replace('/[^a-z0-9]+/i','-',iconv('UTF-8','ASCII//TRANSLIT',$this->input->post('name')));
$config['file_name'] = trim($config['file_name'],'-').now().'.jpg';
$this->load->library('upload', $config);
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('goods', 'Goods', 'required');
$this->form_validation->set_rules('name', 'Name', 'required|max_length[12]');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('campus_write_v');
}
else
{
print_r($_FILES['userfile']);
if (empty($_FILES['userfile'])) {
if(!$query = $this->campus_m->create_review("Marla-overdoses1360186300.jpg")){
$data['write_campus'] = 'The answer has not been stored.';
$this->load->view('campus_write_v', $data);
}
else {
$data['write_campus'] = 'The answer has been stored. ';
$this->load->view('campus_write_v', $data);
}
}
else
{
if($this->upload->do_upload()){
if(!$query = $this->campus_m->create_review($config['file_name'])){
$data['write_campus'] = 'The answer has not been stored.';
$this->load->view('campus_write_v', $data);
}
else {
$data['write_campus'] = 'The answer has been stored. ';
$this->load->view('campus_write_v', $data);
}
}
else
{
$error = array('error' => $this->upload->display_errors());
foreach ($error as $rows => $r)
{
echo $r ;
}
$this->load->view('campus_write_v');
}
}
}
}
And this is the view :
<?php
$attributes = array('id' => 'contactform');
echo form_open_multipart('campus/store', $attributes);
?>
<div>
<?php
echo form_label('Title of Area');
$data = array(
'name' => 'name',
'id' => 'name',
'placeholder' => 'Location and name of the place',
'required' => 'required',
'value' => set_value("name")
);
echo form_input($data);
?>
</div>
<div>
<?php
$data = array(
'name' => 'goods',
'id' => 'goods',
'placeholder' => 'Tell us about the place',
'required' => 'required',
'value' => set_value("goods"),
'rows' => '20',
'cols' => '50'
);
echo form_textarea($data);
?>
</div>
<div>
<input type="file" id="userfile" name="userfile" size="20" />
</div>
<div>
<?php
$data = array(
'class' => 'button',
'value' => 'Submit',
'id' =>"submit"
);
echo form_submit($data);
?>
</div>
<?php echo form_close();
?>
</div>
I will solve this in this way:
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$this->load->library('upload');
$this->upload->initialize($config);
if($this->upload->do_upload('userfile')){
$data = $this->upload->data();
$photo['image'] = $data['file_name']; // Name of image
} else {
$photo['image'] = "Name"; // Name that you want
}
This is the short code of uploads controller.