I have tried to upload the image but it's not working, before adding the upload section code it
was working properly and all the entries from the form are
properly stored in database
but now the form
text field are also not working..
CONTROLLER-
i have tried below code.but its not working.please anyone help me to solve this?
public function index(){
// if(!$this->session->userdata('logged_in')){
// redirect('register/login');
// }
$this->form_validation->set_rules('name', 'Name', 'required');
$this->form_validation->set_rules('location', 'Location', 'required');
$this->form_validation->set_rules('dob', 'DOB', 'required');
$this->form_validation->set_rules('height', 'Height', 'required');
$this->form_validation->set_rules('weight', 'Weight', 'required');
$this->form_validation->set_rules('optselect', 'Complexion', 'required');
$this->form_validation->set_rules('email', 'Email already exists.', 'required');
if($this->form_validation->run() === FALSE){
$this->load->view('templates/pheader');
$this->load->view('profile/aboutme');
$this->load->view('templates/pfooter');
} else {
//Upload Image
$path = './assets/uploads/profilepic';
$config['upload_path'] = $path;
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 15000;
$config['max_width'] = 1920;
$config['max_height'] = 1080;
$this->load->library('upload', $config);
if(!$this->upload->do_upload()){
echo "lol";
echo $this->upload->display_errors();
$this->session->set_flashdata('file_error', $this->upload->display_errors());
$post_image = 'noimage.jpg';
exit();
}else{
$data = array('upload_data' => $this->upload->data());
$post_image = $_FILES['userfile']['name'];
}
$this->profile_model->about($post_image);
redirect('profile/vabout');
}
}
VIEW-
<div class="form-group">
<label class="col-md-3 text-right">Upload Profile Pic:</label>
<div class="col-md-9">
<input type="file" name="userfile" id="userfile" size="20" />
</div>
</div>
MODEL-
public function about($post_image){
$data = array(
'name' => $this->input->post('name'),
'location'=>$this->input->post('location'),
'dob' => $this->input->post('dob'),
'optradio' => $this->input->post('optradio'),
'height'=>$this->input->post('height'),
'weight'=>$this->input->post('weight'),
'optselect'=>$this->input->post('optselect'),
'email' => $this->input->post('email'),
'web'=>$this->input->post('web'),
'awards'=>$this->input->post('awards'),
'cw'=>$this->input->post('cw'),
'pp'=>$this->input->post('pp'),
'youtube'=>$this->input->post('youtube'),
'fb'=>$this->input->post('fb'),
'ld'=>$this->input->post('ld'),
'tw'=>$this->input->post('tw'),
'insta'=>$this->input->post('insta'),
'image'=> $post_image
);
return $this->db->insert('profile', $data);
}
Try This
<form enctype="multipart/form-data" method="post" >
Related
I have an issue updating images for my posts in CodeIgniter.
I have no probleme creating posts with images, but when I try to update a post, post changed info are updated bu not the image.
I have tried several posibilities from different tutorials without success
Thanks in advance
Here is my code:
controller
public function create(){
if(!$this->session->userdata('logged_in'))
{
redirect('users/login');
}
$data['title'] = 'Create Post';
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('body', 'Body', 'required');
if($this->form_validation->run() === FALSE)
{
$this->load->view('templates/header');
$this->load->view('posts/create', $data);
$this->load->view('templates/footer');
}
else
{
$slug = url_title($this->input->post('title'));
$post_image = $this->upload_image();
$data = array(
'title' => $this->input->post('title'),
'slug' => $slug,
'body' => $this->input->post('body'),
'post_image' => $post_image
);
//$this->post_model->create_post($post_image);
$this->post_model->create_post($data);
// Set message
$this->session->set_flashdata('post_created', 'Your post has been created successfully!');
redirect('posts');
}
}
public function edit($slug)
{
$data['post'] = $this->post_model->get_posts($slug);
// Check if logged user has created this post
if($this->session->userdata('user_id') != $this->post_model->get_posts($slug)['user_id'])
{
redirect('posts');
}
$data['categories'] = $this->category_model->get_categories();
if(empty($data['post']))
{
show_404();
}
$data['title'] = 'Edit Post';
$this->load->view('templates/header');
$this->load->view('posts/edit', $data);
$this->load->view('templates/footer');
}
public function update()
{
// Check login
if(!$this->session->userdata('logged_in')){
redirect('users/login');
}
$id=$this->input->post("id");
$slug = url_title($this->input->post('title'));
if( $_FILES['userfile']['name']!="" )
{
$post_image = $this->upload_image();
}
else
{
$post_image=$this->input->post('old');
}
$data = array(
'title' => $this->input->post('title'),
'slug' => $slug,
'body' => $this->input->post('body'),
'post_image' => $post_image
);
//$this->post_model->update_post($post_image);
$this->post_model->update_post($data,$id);
// Set message
$this->session->set_flashdata('post_updated', 'Your post has been updated successfully!');
redirect('posts');
}
public function upload_image()
{
// Upload Image
$config['upload_path'] = './assets/images/posts';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = '2048';
$config['max_width'] = '2000';
$config['max_height'] = '2000';
$this->load->library('upload', $config);
if(!$this->upload->do_upload())
{
$errors = array('error' => $this->upload->display_errors());
$post_image = 'noimage.jpg';
}
else
{
$upload_data=$this->upload->data();
$post_image=$upload_data['file_name'];
}
return $post_image;
}
model:
public function create_post($data)
{
return $this->db->insert('posts', $data);
}
public function update_post($data, $id)
{
$this->db->where('id', $id);
return $this->db->update('posts', $data);
}
view (edit view):
<?php echo form_open('posts/update'); ?>
<input type="hidden" name="id" value="<?php echo $post['id']; ?>">
<input type="hidden" id="old" name="old" value="<?php echo $post['post_image'] ?>">
<div class="form-group">
<label>Title</label>
<input type="text" class="form-control" name="title" placeholder="Add Title" value="<?php echo $post['title']; ?>">
</div>
<div class="form-group">
<label>Body</label>
<textarea id="editor1" class="form-control" name="body" placeholder="Add Body"><?php echo $post['body']; ?></textarea>
</div>
<div class="form-group">
<label>Change Image</label>
<input class="form-control" type="file" name="userfile" size="20">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
I'm not sure how anything is uploading as do_upload() doesn't declare the the name of the file field. In this case I suppose it is userfile e.g. do_upload('userfile').
Consider doing something with you file uploads error variable as this should have uncovered your error rather quickly.
EDIT:
My bad, apparently do_upload() default file name parameter for upload is userfile. My guess now as to why it isn't working is that your "update" form, doesn't use form_open_multipart!
Please note: you can also move your redirect function back to login to the controllers constructor. This will eliminate some lines of duplicate code.
Also if you are looking for an easy way to get the errors of the upload function you can do the following:
private function upload_image()
{
// Upload Image
$config['upload_path'] = './assets/images/posts';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = '2048';
$config['max_width'] = '2000';
$config['max_height'] = '2000';
$this->load->library('upload', $config);
if(!$this->upload->do_upload())
{
throw new Exception($this->upload->display_errors());
//$errors = array('error' => $this->upload->display_errors());
//$post_image = 'noimage.jpg';
}
else
{
$upload_data=$this->upload->data();
$post_image=$upload_data['file_name'];
}
return $post_image;
}
Usage:
try {
$this->upload_image();
} catch (Exception $e) {
show_error($e->getMessage());
}
I'm having a bit of a problem trying to make an upload form in codeigniter.
Not sure what I am doing wrong, everything else is getting correctly in the database. I looked in the documentation and tried several things but I'm not getting any further.. Any feedback is very much appreciated!
Thanks in advance.
model:
public function set_newstudent()
{
$this->load->helper('url');
$slug = url_title($this->input->post('naam'), 'dash', TRUE);
$config['upload_path'] = '/file_path/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$this->load->library('upload', $config);
$this->upload->data('full_path'););
$data_upload_files = $this->upload->data();
$image = $data_upload_files[full_path];
$data = array(
'naam' => $this->input->post('naam'),
'voornaam' => $this->input->post('voornaam'),
'id' => $slug,
'text' => $this->input->post('text'),
'picture'=>$this->input->post('picture')
);
return $this->db->insert('student', $data);
}
controller:
public function create()
{
$this->load->helper('form');
$this->load->library('form_validation');
$data['title'] = 'Create a new Student';
$this->form_validation->set_rules('naam', 'Naam', 'required');
$this->form_validation->set_rules('voornaam', 'Voornaam', 'required');
$this->form_validation->set_rules('text', 'Text', 'required');
if ($this->form_validation->run() === FALSE)
{
$this->load->view('templates/header', $data);
$this->load->view('students/create');
$this->load->view('templates/footer');
}
else
{
$this->student_model->set_newstudent();
$this->load->view('students/success');
}
}
view:
<?php echo validation_errors(); ?>
<?php echo form_open_multipart('student/create');?>
<div class="form-group">
<label for="naam">Naam</label><br>
<input type="input" name="naam" class="form-control" /><br />
</div>
<div class="form-group">
<label for="voornaam">Voornaam</label><br>
<input type="input" name="voornaam" class="form-control"/><br />
</div>
<div class="form-group">
<label for="text">Vertel iets over jezelf:</label><br>
<textarea name="text" class="form-control" rows="5"></textarea><br />
</div>
<div class="form-group">
<label for="text">Kies een profiel foto:</label><br>
<input type="file" name="userfile" class="btn btn-default btn-file" />
</div>
<input type="submit" class="btn btn-success" name="submit"
value="Create student" style="width:100%;margin-bottom:1%" />
</form>
As i see, there is no
$this->upload->do_upload()
That's the function responsible for performing the upload and it's not there in your code,you may want to read this:
File uploading Class
UPDATE
Your code should look something like this,
Controller:
public function create(){
$this->load->helper('form');
$this->load->library('form_validation');
$data['title'] = 'Create a new Student';
$this->form_validation->set_rules('naam', 'Naam', 'required');
$this->form_validation->set_rules('voornaam', 'Voornaam', 'required');
$this->form_validation->set_rules('text', 'Text', 'required');
if ($this->form_validation->run() === FALSE){
$this->load->view('templates/header', $data);
$this->load->view('students/create');
$this->load->view('templates/footer');
}else{
// Upload the files then pass data to your model
$config['upload_path'] = '/file_path/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$this->load->library('upload', $config);
if (!$this->upload->do_upload('userfile')){
// If the upload fails
echo $this->upload->display_errors('<p>', '</p>');
}else{
// Pass the full path and post data to the set_newstudent model
$this->student_model->set_newstudent($this->upload->data('full_path'),$this->input->post());
$this->load->view('students/success');
}
}
}
Model:
public function set_newstudent($path,$post){
$data = array(
'naam' => $post['naam'],
'voornaam' => $post['voornaam'],
'text' => $post['text'],
'picture'=>$path
);
return $this->db->insert('student', $data);
}
The problem is in your code where you are using upload library. You are using the wrong function to upload files. Below is the correct code:
$config['upload_path'] = '/file_path/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$this->load->library('upload', $config);
// Check file uploaded or not
if ($this->upload->do_upload('userfile')) {
$data_upload_files = $this->upload->data();
$image = $data_upload_files[full_path];
} else {
// possibly do some clean up ... then throw an error
}
This code should work.
I have a field of my form (which is uploading personal picture). So the user selects image from pc and submit the form.
Controller:
function do_upload()
{
$config['upload_path'] = './images/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100000';
$config['max_width'] = '10240';
$config['max_height'] = '7680';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
print_r($this->upload->data());
$datafoto = $this->upload->data();
$nm_file = time().$datafoto['orig_name'];
$this->load->model('mkegiatan');
$this->mkegiatan->update_foto($nm_file);
copy('images/'.$datafoto['orig_name'], 'images/'.$nm_file);
}
}
And contoller edit event like this:
function edit_kegiatan()
{
$id_kegiatan = $this->uri->segment(4);
//set validation properties
$this->form_validation->set_rules('tanggal_kegiatan', 'Tanggal', 'required');
$this->form_validation->set_rules('nama_kegiatan', 'Judul Berita', 'required');
$this->form_validation->set_rules('content', 'Content', 'required');
//run validation
// jika dia ingin update data atau form validation error
if ($this->form_validation->run() == FALSE) {
$data = $this->mkegiatan->get_by_id($id_kegiatan);
$this->load->model('mkegiatan');
$config['upload_path'] = './images/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100000';
$config['max_width'] = '10240';
$config['max_height'] = '7680';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('userfile')) //you forgot this
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
//print_r($this->upload->data());
$datafoto=$this->upload->data();
$nm_file = time().$datafoto['orig_name'];
$data = array(
'tanggal_kegiatan' => $this->input->post('tanggal_kegiatan'),
'nama_kegiatan' => $this->input->post('nama_kegiatan'),
'content' => $this->input->post('content'),
'image' => $nm_file
);
$this->mkegiatan->update_kegiatan($id_kegiatan,$data);
$this->session->set_flashdata('message', generateSuccessMessage('Data berhasil diupdate'));
redirect(site_url('admin/kegiatan'));
}
}
$this->data['orang'] = $this->mlogin->dataPengguna($this->session->userdata('username'));
//var_dump($data);
//$tmp_data = array('id_kegiatan' => $id_kegiatan);
$this->data['contents'] = $this->load->view('admin/kegiatan/edit_kegiatan', $this->data, true);
$this->load->view('template/wrapper/admin/wrapper_ukm',$this->data);
}
View:
<?php echo form_open(current_url(),'name=form'); ?>
<div class="fours fields">
<div class="field">
<div class="ui vertical segment">
<div class="date field">
<label>Tanggal</label>
<div class="ui small icon input left">
<input type="text" id="datepicker" placeholder="xxxx-xx-xx" name="tanggal_kegiatan" value="<?php echo $tanggal_kegiatan;?>"><?php echo form_error('tanggal_kegiatan', '<div class="ui red pointing label">', '</div>'); ?>
<i class="calendar icon"></i>
</div>
</div>
</div>
</div>
</div>
<div class="two fields">
<div class="field">
<label>Nama Acara</label>
<div class="ui small left icon input">
<input type="text" placeholder="Nama Kegiatan" name="nama_kegiatan" value="<?php echo $nama_kegiatan;?>"><?php echo form_error('nama_kegiatan', '<div class="ui red pointing label">', '</div>'); ?>
<i class="text file outline icon"></i>
</div>
</div>
</div>
<div class="field">
<label>Isi Kegiatan</label>
<textarea placeholder="Text" name="content">
<?php echo $content;?>
</textarea><?php echo form_error('content', '<div class="ui red pointing label">', '</div>'); ?>
</div>
<input type="file" name="userfile" size="20">
<input class="ui small blue submit button" type="submit" value="Save">
</form>
Do I need to create two separate forms to accomplish this (one for the image upload and one for the text input)? Or is it possible to write a function in the controller that can validate and process both the upload and text input simultaneously?
try this
function tambah_kegiatan()
{
//set validation properties
$this->form_validation->set_rules('nama_kegiatan', 'Judul Berita', 'required');
if ($this->form_validation->run() == true)
{
$this->load->model('mkegiatan');
$config['upload_path'] = './images/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100000';
$config['max_width'] = '10240';
$config['max_height'] = '7680';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('userfile')) //you forgot this
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
print_r($this->upload->data());
$datafoto=$this->upload->data();
$nm_file = time().$datafoto['orig_name'];
$data = array(
'nama_kegiatan' => $this->input->post('nama_kegiatan'),
'image' => $nm_file
);
$this->mkegiatan->insert_kegiatan($data);
$this->session->set_flashdata('message', generateSuccessMessage('Data berhasil ditambah'));
redirect(site_url('admin/kegiatan'));
}
}
$this->data['orang'] = $this->mlogin->dataPengguna($this->session->userdata('username'));
$this->data['contents'] = $this->load->view('admin/kegiatan/tambah_kegiatan', '', true);
$this->load->view('template/wrapper/admin/wrapper_ukm',$this->data);
}
and your image folder having all permissions
function edit_kegiatan($id_kegiatan='')
{
//set validation properties
$this->form_validation->set_rules('tanggal_kegiatan', 'Tanggal', 'required');
$this->form_validation->set_rules('nama_kegiatan', 'Judul Berita', 'required');
$this->form_validation->set_rules('content', 'Content', 'required');
//run validation
// jika dia ingin update data atau form validation error
if ($this->form_validation->run() == FALSE) {
$data = $this->mkegiatan->get_by_id($id_kegiatan);
$this->load->model('mkegiatan');
$config['upload_path'] = './images/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100000';
$config['max_width'] = '10240';
$config['max_height'] = '7680';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('userfile')) //you forgot this
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
//print_r($this->upload->data());
$datafoto=$this->upload->data();
$nm_file = time().$datafoto['orig_name'];
$data = array(
'tanggal_kegiatan' => $this->input->post('tanggal_kegiatan'),
'nama_kegiatan' => $this->input->post('nama_kegiatan'),
'content' => $this->input->post('content'),
'image' => $nm_file
);
$this->mkegiatan->update_kegiatan($id_kegiatan,$data);
$this->session->set_flashdata('message', generateSuccessMessage('Data berhasil diupdate'));
redirect(site_url('admin/kegiatan'));
}
}
$this->data['orang'] = $this->mlogin->dataPengguna($this->session->userdata('username'));
//var_dump($data);
//$tmp_data = array('id_kegiatan' => $id_kegiatan);
$this->data['contents'] = $this->load->view('admin/kegiatan/edit_kegiatan', $this->data, true);
$this->load->view('template/wrapper/admin/wrapper_ukm',$this->data);
}
This question already has answers here:
Upload multiple files in CodeIgniter
(7 answers)
Closed 8 years ago.
I'm a new with codeigniter, can anyone help me on muliptle image uploads. the scenario is, validate each image then if there are errors, show each message below each input file. if there's no error upload and save the file name to database. i really need a basic way on how to do this. I've tried anything but no luck. Any help is very much appreciated. thanks in advance!
this is my controller
hotel.php
public function add()
{
//if(!is_ajax_request()) return;
$this->set_validation_rules();
if($this->form_validation->run())
{
$m_insert = $this->get_posted_hotel_data();
$hotel_id = $this->hotel_model->insert($m_insert);
$this->upload_image($hotel_id);
redirect('hotel');
}
else
{
$data['accept'] = array( 0 => 'False', 1 => 'True');
$data['destinations'] = prepare_dropdown_array($this->destination_model->get(), 'cde_id', 'cde_name', '--please select');
echo $this->load->view('form_hotel_add', $data);
}
}
private function set_validation_rules()
{
$this->form_validation->set_rules('destination', '', 'required');
$this->form_validation->set_rules('stars', '', 'required|numeric');
$this->form_validation->set_rules('name', '', 'required');
$this->form_validation->set_rules('address', '', 'required');
$this->form_validation->set_rules('email', '', 'required|valid_email');
$this->form_validation->set_rules('phone', '', 'required|numeric');
$this->form_validation->set_rules('tm_comments', '', 'required');
$this->form_validation->set_rules('am_comments', '', 'required');
$this->form_validation->set_rules('accept[]', '', 'required');
$this->form_validation->set_rules('room', '', 'required');
$this->form_validation->set_rules('location', '', 'required');
$this->form_validation->set_rules('amenities', '', 'required');
$this->form_validation->set_rules('image1', 'Image1', 'callback__handle_upload');
$this->form_validation->set_rules('image2', 'Image2', 'callback__handle_upload');
$this->form_validation->set_rules('image3', 'Image3', 'callback__handle_upload');
$this->form_validation->set_message('required', 'This field is required.');
}
private function get_posted_hotel_data()
{
$data1 = array('cho_destination' => $this->input->post('destination'),
'cho_stars' => $this->input->post('stars'),
'cho_name' => $this->input->post('name'),
'cho_address' => $this->input->post('address'),
'cho_email' => $this->input->post('email'),
'cho_phone' => $this->input->post('phone'),
'cho_tm_comment' => $this->input->post('tm_comments'),
'cho_am_comment' => $this->input->post('am_comments'),
'cho_rooms' => $this->input->post('room'),
'cho_location' => $this->input->post('location'),
'cho_amenities' => $this->input->post('amenities'));
foreach($_POST['accept'] as $v)
{
$data2 = array("cho_accept" => $v);
}
$data = array_merge($data1, $data2);
return $data;
}
private function upload_image($id)
{
$ctr=1;
$x=1;
foreach($_FILES as $key => $value)
{
//print_r($_FILES);
//die();
if(!empty($value['name']))
{
$config['upload_path'] = './uploads/hotels';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1024';
$config['encrypt_name'] = true;
//$config['file_name'] = sprintf('%s_%s', $id, $value['name']);
$filename=explode('.', $value['name']);
$config['file_name'] = sprintf('%s_%s', $id, 'image_'.$ctr.'.'.$filename[1]);
$config['overwrite'] = TRUE;
$this->load->library('upload', $config);
if(move_uploaded_file($_FILES["image".$x]["tmp_name"],"uploads/hotels/" . $config['file_name']))
{
$uploaded = $this->upload->data();
//Create Thumbnail
$img_lib['image_library'] = 'gd2';
$img_lib['source_image'] = $uploaded['full_path'];
$img_lib['master_dim'] = 'width';
$img_lib['quality'] = 75;
$img_lib['maintain_ratio'] = TRUE;
$img_lib['width'] = 380;
$img_lib['height'] = 280;
$this->load->library('image_lib', $img_lib);
$this->image_lib->clear();
$this->image_lib->initialize($img_lib);
$this->image_lib->resize();
}
}
if(!empty($_FILES['image'.$x]['tmp_name']))
{
$this->add_image($id, $config['file_name'], $x);
}
$ctr++;
$x++;
}
}
public function _handle_upload()
{
$y=1;
foreach($_FILES as $key=> $val)
{
if(empty($_FILES['image'.$y]))
{
$this->form_validation->set_message('_handle_upload', "You must upload an image!");
return false;
}
$y++;
}
}
private function add_image($id, $image, $x)
{
$data = array('cho_image'.$x => $image);
$this->hotel_model->update($id, $data);
}
And this is my view:
form_hotel_add.php
<div class="control-group">
<label for="image1" class="control-label">Image 1</label>
<div class="controls">
<input type="file" id="image1" name="image1" accept="image/*" />
<?=form_error('image1', '<br><label class="error">','</label>')?>
</div>
</div>
<div class="control-group">
<label for="image2" class="control-label">Image 2</label>
<div class="controls">
<input type="file" id="image2" name="image2" accept="image/*"/>
<?=form_error('image2', '<br><label class="error">','</label>')?>
</div>
</div>
<div class="control-group">
<label for="image3" class="control-label">Image 3</label>
<div class="controls">
<input type="file" id="image3" name="image3" accept="image/*" />
<?=form_error('image3', '<br><label class="error">','</label>')?>
</div>
</div>
you need not to use move_upload_file()
use do_upload() method in upload class of codeigniter
I want to know how can I upload an image and save its name in database when using codeigniter,
I am using simple form tag for this, like,
<form action="" name="add" enctype="multipart/form-data" method="post">
<label for="img">Image: </label> <input type="file" name="image" />
<label for="title">Title: </label> <input type="text" name="photo_title" value="" />
<label for="description">Description: </label>
<textarea name="photo_description"> </textarea>
<label for="photo_category">Category </label>
<select name="photo_category">
<option value="select_one">Please Select..</option>
<option value="fun">Fun</option>
</select>
<input type="submit" value="Add" />
</form>
I check the manual but can't get the point what they are doing , anybody please help me to make a controler function for this that will insert all the values in database and also upload the image file.
Thanks,
Shah Rukh
public function upload() {
$this->load->library('form_validation');
$this->form_validation->set_rules('photo_title', 'Photo Title', 'required');
$this->form_validation->set_rules('photo_description', 'Photo Description', 'required');
$this->form_validation->set_rules('photo_category', 'Photo Category', 'required');
if ($this->form_validation->run() == TRUE) {
$data = array(
'photo_title' => $this->form_validation->set_value('photo_title'),
'photo_description' => $this->form_validation->set_value('photo_description'),
'photo_category' => $this->form_validation->set_value('photo_category'),,
);
$this->db->insert('table_name', $data);
// Start Upload Picture
if (!empty($_FILES['image']['name'])) {
$config['upload_path'] = ''.$_SERVER['DOCUMENT_ROOT'].'/upload_path/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '5000';
$config['max_width'] = '2000';
$config['max_height'] = '2000';
$config['encrypt_name'] = TRUE;
$config['remove_spaces'] = TRUE;
$this->load->library('upload', $config);
if ($this->upload->do_upload('image')) {
$data = array('upload_data' => $this->upload->data());
$data = $this->upload->data();
$this->db->select('id');
$this->db->from('table_name');
$this->db->order_by('id', 'DESC');
$this->db->limit(1);
$query = $this->db->get();
$row = $query->row();
$data1 = array(
'shirt' => $data['file_name'],
);
$this->db->where('id', $row->id);
$this->db->update('table_name', $data1);
}
}
};
}
by using
$this->upload->initialize($config);
instead of
$this->load->library('upload', $config);
Thanks,
Shah Rukh