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());
}
Related
How to upload images and videos in two different folders using CodeIgniter 3?
I have done only images. Please guide me through videos. I want to upload image and videos in two different folders.
My Controller, Model and View
class Blog extends CI_Contrller{
public function create_blog(){
// Check Login
if(!$this->session->userdata('logged_in')){
redirect('blogs/login');
}
$data['title'] = 'Create Blog';
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('content', 'Content', 'required');
if($this->form_validation->run() === FALSE){
$this->load->view('templates/header');
$this->load->view('blog/create_blog', $data);
$this->load->view('templates/footer');
} else {
// Upload Image
$path = './assets/uploads/blogs/images';
$config['upload_path'] = $path;
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 15000;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
if(!$this->upload->do_upload()){
$this->session->set_flashdata('file_error', $this->upload->display_errors());
$post_image = 'noimage.jpg';
redirect('blog/create_blog');
}else{
$data = array('upload_data' => $this->upload->data());
$post_image = $_FILES['userfile']['name'];
}
$this->M_blog->create_blog($post_image);
redirect('blog/view');
}
}
}
Model
// Model Begins here
public function create_blog($post_image){
$slug = url_title($this->input->post('title'));
$data = array(
'title' => $this->input->post('title'),
'slug' => $slug,
'content' => $this->input->post('content'),
'image' => $post_image
);
return $this->db->insert('events', $data);
}
View
// View begins here
<div class="container"><br>
<h2><?= $title ?></h2>
<?php echo form_open_multipart('blog/create_blog'); ?>
<?php echo validation_errors(); ?>
<?php echo $this->session->flashdata('file_error'); ?>
<div class="form-group">
<label>Title</label>
<input type="text" class="form-control" name="title"
placeholder="Add Title">
</div>
<div class="form-group">
<label>Body</label>
<textarea class="form-control" id="editor1" name="content"
placeholder="Add Body"></textarea>
</div>
<div class="form-group">
<label>Upload Image</label>
<input type="file" name="userfile" id="userfile" size="20" />
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
</div>
Try this
class Blog extends CI_Contrller{
function create_blog() {
// Check Login
if (!$this->session->userdata('logged_in')) {
redirect('blogs/login');
}
$data['title'] = 'Create Blog';
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('content', 'Content', 'required');
if ($this->form_validation->run() === FALSE) {
$this->load->view('templates/header');
$this->load->view('blog/create_blog', $data);
$this->load->view('templates/footer');
} else {
// Upload Image
$ext = pathinfo($_FILES['userfile']['name'], PATHINFO_EXTENSION);
$ext = strtolower($ext);
$image_ext_arr = array('gif', 'jpg', 'png', 'jpeg');
if (in_array($ext, $image_ext_arr)) {
$path = FCPATH.'assets/uploads/blogs/images';
$allowed_types = 'gif|jpg|png';
} else {
$path = FCPATH.'assets/uploads/blogs/video';
$allowed_types = 'mp4|mp3';
}
$config['upload_path'] = $path;
$config['allowed_types'] = $allowed_types;
$config['max_size'] = 15000;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
if (!$this->upload->do_upload()) {
$this->session->set_flashdata('file_error', $this->upload->display_errors());
$post_image = 'noimage.jpg';
redirect('blog/create_blog');
} else {
$data = array('upload_data' => $this->upload->data());
$post_image = $_FILES['userfile']['name'];
}
$this->M_blog->create_blog($post_image);
redirect('blog/view');
}
}
}
Added few line :
$ext = pathinfo($_FILES['userfile']['name'], PATHINFO_EXTENSION); //to find whether is image or video
$ext = strtolower($ext);// some time extension in upper later so converted into small letters
$image_ext_arr = array('gif', 'jpg', 'png', 'jpeg'); //give here all extension of image that you want to upload
if (in_array($ext, $image_ext_arr)) { //if matches means is image
$path = FCPATH.'assets/uploads/blogs/images';
$allowed_types = 'gif|jpg|png';
} else { //else video
$path = FCPATH.'assets/uploads/blogs/video';
$allowed_types = 'mp4|mp3';
}
Note : Better to give absolute path instead of relative path to upload so
$path = FCPATH.'assets/uploads/blogs/video';
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'm stuck from make a update image from codeigniter, i want replace image when i update it but, the code its doesn't work.
here is my code :
this is Controller :
public function updatefoto(){
$this->load->model('model_users');
$this->model_users->updatefoto();
}
This is The Model
public function updatefoto($userId){
if($this->input->post('submit')){
$config['upload_path'] = "./uploads/images/";
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '2000';
$config['max_width'] = '2000';
$config['max_height'] = '2000';
$config['file_name'] = 'gambar-'.trim(str_replace(" ","",date('dmYHis')));
$this->load->library('upload', $config);
if (!$this->upload->do_upload("gambar")) {
$error = array('error' => $this->upload->display_errors('<div class="alert alert-danger">×','</div>'));
$this->load->view('view_ubah_data-profile', $error);
}else{
$nama=$this->upload->data('file_name');
$this->db->where('id', $userId);
$this->db->update('foto',array('nama_foto'=>$nama));
redirect('view_ubah_data-profile','refresh');
}
}
}
and the last is view
<div class="col-md-4 col-sm-6 col-xs-12">
<form action="users/updatefoto" method="post" id="updateUserImage">
<div class="text-center">
<img src="<?php echo base_url();?>uploads/images/<?php echo $userData['nama_foto'] ?>" cclass="img-thumbnail" alt="avatar">
<h6>Upload a different photo...</h6>
<input type="file" class="text-center center-block well well-sm" name="gambar" id="files" accept="image/*" required>
<button type="submit" value="Upload" name="submit" class="btn btn-primary">
<i class="fa fa-upload" aria-hidden="true"></i>upload
</button>
</div>
</form>
</div>
Thanks I appreciate any help :)
Here is how I do it.
Edit- Class Construct
class Team extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->library('form_validation');
$this->load->helper(array('form', 'url'));
$this->load->model('team_model');
}
public function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 100;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('userfile1'))
{
$data['errors'] = array('error' => $this->upload->display_errors());
$data['client_Id']=$this->team_model->getUserId($email);
$data['pic']=$this->team_model->getImage($data['client_Id']['id']);
// Here I load my Views
}
else
{
$upload_data = $this->upload->data();
$file_name=$upload_data['file_name'];
$email=$this->session->userdata['email'];
$data['client_Id']=$this->team_model->getUserId($email);
$data['pic']=$this->team_model->getImage($data['client_Id']['id']);
// If there is no image insert one other wise update
if($data['pic']['image']!=''){
$this->team_model->updateEmployeeImage($data['pic']['id'],$file_name);
$data['success']='Congratulations! Image Updated Successfully';
}else{
$this->team_model->InsertEmployeeImage($data['client_Id']['id'],$file_name);
$data = array('upload_data' => $this->upload->data());
$data['success']='Congratulations! Image Uploaded Successfully';
}
$file_name=$upload_data['file_name'];
$email=$this->session->userdata['email'];
$data['client_Id']=$this->team_model->getUserId($email);
$data['pic']=$this->team_model->getImage($data['client_Id']['id']);
// Here I load my Views
}
}
}
Edit- Model Functions
public function InsertEmployeeImage($id,$image){
$Image=array(
'id' => '',
'team_id' => $id,
'image' => $image
);
$this->db->insert('teams_image',$Image);
return ;
}
public function updateEmployeeImage($id,$img){
$this->deleteOldImage($id);
$pic=array(
'image' => $img
);
$this->db->WHERE('id',$id)->update('teams_image',$pic);
}
I would like to know with my code how am I able to make my file upload required. Because codeigniter form validation set rules is for post only.
I am unsure on how to make the file upload a requirement on form submit so it checks if file is required. And lets me submit form if file is upload or something along those lines
The callback works fine but required function cannot get to work with file upload.
Controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Store_add extends MX_Controller {
public function __construct() {
parent::__construct();
if (!$this->session->userdata('isLogged')) {
redirect('admin');
}
$this->load->model('admin/setting/model_store_add');
$this->load->model('admin/setting/model_store_get');
$this->lang->load('admin/setting/store_add', $this->settings->get('config_admin_language'));
}
public function index() {
// Title Language
$this->document->setTitle($this->lang->line('heading_title'));
$this->load->library('form_validation');
$this->form_validation->set_rules('config_image', 'Store Image', 'callback_do_upload_image');
if ($this->form_validation->run($this) == FALSE) { // "$this is for HMVC MY_Form_validation"
return $this->load->view('setting/store_add.tpl');
} else {
$store_id = $this->model_store_add->add_store();
$this->do_upload_image($store_id);
$this->session->set_flashdata('success', $this->lang->line('text_success'));
redirect('admin/setting/store');
}
}
public function do_upload_image($store_id) {
$config['upload_path'] = './image/upload/';
$config['allowed_types'] = $this->settings->get('config_file_ext_allowed');
$config['max_size'] = $this->settings->get('config_file_max_size');
$config['overwrite'] = $this->settings->get('config_file_overwrite');
$config['max_width'] = '*';
$config['max_height'] = '*';
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (!$this->upload->do_upload('config_image')) {
$this->load->library('form_validation');
$this->form_validation->set_message('do_upload_image', $this->upload->display_errors('<b>config_image</b>' .' '));
return false;
} else {
if ($store_id) {
$this->model_store_add->add_config_image($store_id, $this->upload->data());
}
}
}
}
Sample View form
<div class="panel-body">
<?php $data = array('id' => 'form-store-add', 'role' => 'form', 'class' => 'form-horizontal' );?>
<?php echo form_open_multipart('admin/setting/store/add', $data);?>
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<?php echo validation_errors('<div class="alert alert-danger">', '</div>'); ?>
<?php echo $this->load->view('setting/store_flashdata.tpl');?>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label" for="input-image"><?php echo $entry_image; ?></label>
<div class="col-sm-10">
<input type="hidden" name="config_image" value="<?php echo set_value('config_image', '');?>">
<input type="file" name="config_image" size="20"/>
</div>
</div>
<?php echo form_close();?>
</div>
WORKING NOW: I did more research on http://php.net/ I have fixed form validation for file upload made sure it is required I added is_file_upload and also added set form validation message with a return of false
public function do_upload_image($store_id) {
$config['upload_path'] = './image/upload/';
$config['allowed_types'] = $this->settings->get('config_file_ext_allowed');
$config['max_size'] = $this->settings->get('config_file_max_size');
$config['overwrite'] = $this->settings->get('config_file_overwrite');
$config['max_width'] = '*';
$config['max_height'] = '*';
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (is_uploaded_file($_FILES['config_image']['tmp_name'])) {
if (!$this->upload->do_upload('config_image')) {
$this->load->library('form_validation');
$this->form_validation->set_message('do_upload_image', $this->upload->display_errors('<b>config_image</b>' .' '));
return false;
} else {
if ($store_id) {
$this->model_store_add->add_config_image($store_id, $this->upload->data());
}
}
} else {
$this->form_validation->set_message('do_upload_image', 'Image Required');
return false;
}
}
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);
}