Codeigniter:Update Image and Display - codeigniter

I'm stuck on a problem with updating image. I've created image upload which works fine but I also want it to be updated. When I add a need image it updates correctly but I if don't want to change the image and leave it as it is, then my current image can't be retrieve. Please help me
Controller
public function insert()
{
$data['s_em']=$this->input->post('s_em');
$data['s_na']=$this->input->post('s_na');
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg|jpe|pdf|doc|docx|rtf|text|txt';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('file'))
{
$error = array('error' => $this->upload->display_errors());
}
else
{
$data['file']=$this->upload->data('file_name');
}
$this->Students_m->db_ins($data);
$this->load->view('admin/newstudents');
}
public function edit($id)
{
$dt['da']=$this->Students_m->db_edit($id)->row_array();
$this->load->view('admin/st_edt',$dt);
}
public function update()
{
$id=$this->input->post("id");
$s_em=$this->input->post("s_em");
$s_na=$this->input->post("s_na");
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg|jpe|pdf|doc|docx|rtf|text|txt';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('file'))
{
$error = array('error' => $this->upload- >display_errors());
}
else
{
$upload_data=$this->upload->data();
$image_name=$upload_data['file_name'];
}
$data=array('s_em'=>$s_em,'s_na'=>$s_na,'file'=>$image_name);
$this->Students_m->db_update($data,$id);
}
redirect("admin/students");
}
Model
public function db_ins($data)
{
$query=$this->db->insert('student',$data);
return $query;
}
public function db_edit($id)
{
return $this->db->get_where('student', array('id'=>$id));
}
public function db_update($data,$id)
{
$this->db->where('id', $id);
$this->db->update('student', $data);
}
view
<form action="../update" method="post" enctype="multipart/form-data">
<legend class="text-semibold">Personal data</legend>
<img src=" <?php echo base_url( 'uploads/'. $da['file']);?>" height="205" width="205">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label class="display-block">image:<span class="text-danger">*</span></label>
<input name="file" type="file" id="image_id" class="file-styled ">
<span class="help-block"></span>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>Email address: <span class="text-danger">*</span></label>
<input type="email" name="s_em" class="form-control required" value="<?php echo $da['s_em'];?>">
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label>Student name: <span class="text-danger">*</span></label>
<input type="text" name="s_na" class="form-control required" value="<?php echo $da['s_na'];?>" id="n1">
</div>
</div>
<button type="submit">Update<i class="icon-check position-right"></i></button>
<input type="hidden" name="id" value="<?php echo $da['id'] ;?>">
</form>
</div>

public function update()
{
$id=$this->input->post("id");
$s_em=$this->input->post("s_em");
$s_na=$this->input->post("s_na");
if($_FILES[file]['name']!="")
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg|jpe|pdf|doc|docx|rtf|text|txt';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('file'))
{
$error = array('error' => $this->upload- >display_errors());
}
else
{
$upload_data=$this->upload->data();
$image_name=$upload_data['file_name'];
}
}
else{
$image_name=$this->input->post('old');
}
$data=array('s_em'=>$s_em,'s_na'=>$s_na,'file'=>$image_name);
$this->Students_m->db_update($data,$id);
}
in the view file add the following
<input type="hidden" id="old" name="old" value="<?php echo $da['file'] ?>">
try this..let me know this works or not.

Be always careful when you upload image with a same name. It's uploaded successfully but you can't see the if changed on the front end of it has same URL and same name that is because your browser have Cached previous Image.
Make sure image is updated successfully on server first

Related

How to input multiple value with checkbox in CodeIgniter?

I have prepared the form to be inputted to the database, but specifically for multiple checkboxes. I've found a similar case with the solutionbut not with the algorithm that I use
Here it is my controller
class Ruang extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->model("m_ruang");
$this->load->library('form_validation');
if($this->session->userdata('status') != "login"){
redirect(base_url("login"));
}
}
public function index()
{
$data["ruang"] = $this->m_ruang->getAll();
$this->load->view('admin/ruang/index.php', $data);
}
public function add()
{
$ruang = $this->m_ruang;
$validation = $this->form_validation;
$validation->set_rules($ruang->rules());
if ($validation->run()) {
$ruang->save();
$this->session->set_flashdata('success', 'Berhasil ditambahkan');
}
$this->load->view("admin/ruang/add_ruang");
}
Here it is my models
class M_ruang extends CI_Model
{
private $_table = "ruang";
public $id_ruang;
public $ruang;
public $kapasitas_kuliah;
public $kapasitas_ujian;
public $layout;
public $fasilitas;
public function getAll()
{
return $this->db->get($this->_table)->result();
}
public function getById($id)
{
return $this->db->get_where($this->_table, ["id_ruang" => $id])->row();
}
public function save()
{
$post = $this->input->post();
$this->id_ruang = uniqid();
$this->ruang = $post["ruang"];
$this->kapasitas_kuliah = $post["kapasitas_kuliah"];
$this->kapasitas_ujian = $post["kapasitas_ujian"];
$this->layout = $post["layout"];
$this->fasilitas = $post["fasilitas"];
$this->db->insert($this->_table, $this);
}
and here part of form view
<form action="<?php base_url('ruang/add') ?>" method="post" enctype="multipart/form-data" >
<div class="form-group">
<label for="ruang">Nama Ruang</label>
<input class="form-control <?php echo form_error('ruang') ? 'is-invalid':'' ?>"
type="text" name="ruang" placeholder="Masukkan nama ruangan" />
<div class="invalid-feedback">
<?php echo form_error('ruang') ?>
</div>
</div>
<div class="form-group">
<label for="kapasitas_kuliah">Kapasitas Kuliah</label>
<input class="form-control <?php echo form_error('kapasitas_kuliah') ? 'is-invalid':'' ?>"
type="number" name="kapasitas_kuliah" min="0" placeholder="Tentukan kapasitas kuliah" />
<div class="invalid-feedback">
<?php echo form_error('kapasitas_kuliah') ?>
</div>
</div>
<div class="form-group">
<label for="kapasitas_ujian">Kapasitas Kuliah</label>
<input class="form-control <?php echo form_error('kapasitas_ujian') ? 'is-invalid':'' ?>"
type="number" name="kapasitas_ujian" min="0" placeholder="Tentukan kapasitas ujian" />
<div class="invalid-feedback">
<?php echo form_error('kapasitas_ujian') ?>
</div>
</div>
<div class="form-group">
<label for="layout">Layout</label>
<input class="form-control"
data-inputmask="'mask': ['99 x 99']" data-mask
type="text" name="layout" placeholder="Tentukan layout ruangan" />
</div>
<div class="form-group">
<label for="fasilitas">Fasilitas Tersedia</label> <br>
<input type="checkbox" name="fasilitas[]" value="Proyektor"> Proyektor
<br>
<input type="checkbox" name="fasilitas[]" value="Papan Tulis"> Papan Tulis
<br>
<input type="checkbox" name="fasilitas[]" value="Jam Dinding"> Jam Dinding
<br>
<input type="checkbox" name="fasilitas[]" value="AC"> AC
<br>
<input type="checkbox" name="fasilitas[]" value="Kipas Angin"> Kipas Angin
<br>
<input type="checkbox" name="fasilitas[]" value="Tong Sampah"> Tong Sampah
<div class="invalid-feedback">
<?php echo form_error('fasilitas') ?>
</div>
</div>
<input class="btn btn-success" type="submit" name="btn" value="Save" />
</form>
This really hinders my project, I hope someone can help
You can use the following line too :
$fasilitas = implode(',', $this->input->post( 'fasilitas' , TRUE ) );
If you can save fasilitas in your database as string. Then you can implode fasilitas array with comma separated as shown below:
$this->fasilitas = implode(',',$post["fasilitas"]);
it will stored in back-end side(Database) something like that.
Proyektor,Papan Tulis
I hope this will works for you.
You Can Use This to Get fasilitas as array :
$fasilitas = $this->input->post('fasilitas'); // Like array('AC','Proyektor','Kipas Angin');
In order for you to get all the checked boxes store in database, write this code.
$values = $post['fasilitas'];
$fasilitas = "";
foreach($values as $val)
{
$fasilitas .= $val . ", ";
}
Then store $fasilitas to db.
$data = array(
'fasilitas' => $fasilitas,
);
$this->db->insert('table_name', $data);
Hope that helps :)

page blashes while redirecting a form with codeigniter

I am learning to develop with codeigniter 3.1.8, for that I am confronted with a problem: I am confronted with a problem: I am redirected in a blank page and my errors do not display. please I would like to give you a hand
here is the function of the model for insertion
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class M_root extends CI_Model{
function isertion_root(){
$root=array(
'nom_admin'=>'$user_name',
'prenom_admin'=>'$user_prenom',
'email_admin'=>'$user_email',
'date_nais_admin'=>'$date_nais',
'date_inscription_admin'=>'NOW(),false',
'avatar_root'=>'$avatar_root',
'password_root'=>'$pssword_root'
);
$this->db->insert('root', $root);
}
/**
* #param $email
* #param $password
* #return bool
*/
function login_root($email,$password){
$this->db->select('*');
$this->db->from('root');
$this->db->where('email_admin',$email);
$this->db->where('password_root',$password);
if ($query=$this->db->get())
{
return $query->row_array();
}
else{
return false;
}
}
public function email_check($email_admin){
$this->db->select('*');
$this->db->from('root');
$this->db->where('email_admin',$email_admin);
$query=$this->db->get();
if($query->num_rows()>0){
return false;
}else{
return true;
}
}
}
here is the function of the controller for insertion
class Root extends CI_Controller{
function __construct(){
parent:: __construct();
$this->load->database();
$this->load->model('M_root');
$this->load->library('upload');
$this->load->library('form_validation');
$this->load->helper(array('form', 'url'));
$this->load->library('session');
}
public function index()
{
$this->load->view("register.php");
}
//debut fonction d'enregistrement
public function isertion_root(){
if($this->input->post('registersubmit')){
$config['upload_path'] = './assets/avatar-root/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 100;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->upload->initialize($config);
if(!empty($_FILES['filefoto']['name']))
{
if ($this->upload->do_upload('filefoto'))
{
$gbr = $this->upload->data();
//Compress Image
$config['image_library']='gd2';
$config['source_image']='./assets/avatar-root/'.$gbr['file_name'];
$config['create_thumb']= FALSE;
$config['maintain_ratio']= FALSE;
$config['quality']= '60%';
$config['new_image']= './assets/avatar-root/'.$gbr['file_name'];
$this->load->library('image_lib', $config);
$this->image_lib->resize();
$this->form_validation->set_rules('nom_admin', 'nom administrateur', 'trim|required|min_length[3]|max_length[23]|alpha_dash|encode_php_tags|xss_clean');
$this->form_validation->set_rules('prenom_admin', 'prenom administrateur', 'trim|required|alpha_dash|encode_php_tags|xss_clean');
$this->form_validation->set_rules('email_admin', 'Email administrateur', 'trim|required|valid_email|xss_clean');
$this->form_validation->set_rules('date_nais_admin', 'Date de naissance administateur', 'trim|required');
$this->form_validation->set_rules('avatar_root', 'Avatar root', 'trim|required');
$this->form_validation->set_rules('password_root', 'password root', 'trim|required|min_length[8]|xss_clean');
$this->form_validation->set_rules('conf_password', 'confirm password', 'trim|required|matches[password]');
$root = array(
'nom_admin' => strip_tags($this->input->post('nom_admin')),
'prenom_admin' => strip_tags($this->input->post('prenom_admin')),
'email_admin' => strip_tags($this->input->post('email_admin')),
'date_nais_admin' => strip_tags($this->input->post('date_nais_admin')),
'avatar_root' => strip_tags($this->input->post('avatar_root')),
'password_root' => password_hash($this->input->post('password'),PASSWORD_DEFAULT)
);
print_r($root);
if ($this->form_validation->run() == TRUE)
{
$email_check=$this->M_root->email_check($root['email_admin']);
if($email_check){
$this->M_root->isertion_root($root);
$this->session->set_flashdata('success_msg', 'Registered successfully.Now login to your account.');
redirect('root/login_view');
}
}
else{
$this->load->view('register');
}
}
}
}
}
//fin fonction d'enregistrement
view
<span style="background-color:red;">
<div class="container">
<div class="row">
<div class="col-md-4 col-md-offset-4">
<div class="login-panel panel panel-success">
<div class="panel-heading">
<h3 class="panel-title">Creation d'un compte</h3>
</div>
<div class="panel-body">
<p class="alert-danger"><?php
$error_msg=$this->session->flashdata('error_msg');
if($error_msg){
echo $error_msg;
}
?></p>
<?php echo validation_errors(); ?>
<form role="form" method="post" enctype="multipart/form-data" autocomplete="off" action="<?php echo base_url().'Root/isertion_root'?>">
<fieldset>
<div class="form-group">
<?php echo form_error('nom_admin'); ?>
<input class="form-control" placeholder="Votre nom" name="nom_admin" value="<?php echo set_value('nom_admin'); ?>"type="text" autofocus>
</div>
<div class="form-group">
<input class="form-control" placeholder="Votre prenom" name="prenom_admin" type="text" autofocus>
</div>
<div class="form-group">
<input class="form-control" placeholder="E-mail" name="email_admin" type="email" autofocus>
</div>
<div class="form-group">
<div class="form-group">
<input class="form-control" placeholder="votre date de naissance" name="date_nais_admin" type="date" value="">
</div>
<div class="form-group">
<input class="form-control" placeholder="mot de passe " name="password_root" type="password" value="">
</div>
<div class="form-group">
<input class="form-control" placeholder="confirmez votre mot de passe" name="conf_password" type="password" value="">
</div>
<div class="form-group">
<input class="form-control" placeholder="votre photo" name="avatar_root" type="file" value="">
</div>
<input class="btn btn-lg btn-success btn-block" type="submit" value="Register" name="registersubmit" >
</fieldset>
</form>
<center><b>Already registered ?</b> <br></b>Login here</center><!--for centered text-->
</div>
</div>
</div>
</div>
</div>
</span>
Once i faced with this same problem i rectified by using this please try this.
ini_set('display_errors', 1);
Your original code contained many ifs and no elses. This mean if an if condition evaluates to false there is no reason for anything to be displayed.
This is a better way of doing it. If you keep everything all in one function and avoid redirects you can properly repopulate the input fields when an error occurs either from form validation or image uploading.
public function index() {
if ($this->input->post('registersubmit')) {
$this->form_validation->set_rules('nom_admin', 'nom administrateur', 'trim|required|min_length[3]|max_length[23]|alpha_dash|encode_php_tags|xss_clean');
$this->form_validation->set_rules('prenom_admin', 'prenom administrateur', 'trim|required|alpha_dash|encode_php_tags|xss_clean');
$this->form_validation->set_rules('email_admin', 'Email administrateur', 'trim|required|valid_email|xss_clean');
$this->form_validation->set_rules('date_nais_admin', 'Date de naissance administateur', 'trim|required');
$this->form_validation->set_rules('avatar_root', 'Avatar root', 'trim|required');
$this->form_validation->set_rules('password_root', 'password root', 'trim|required|min_length[8]|xss_clean');
$this->form_validation->set_rules('conf_password', 'confirm password', 'trim|required|matches[password]');
if ($this->form_validation->run()) {
$this->load->library('upload');
$config['upload_path'] = './assets/avatar-root/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 100;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->upload->initialize($config);
if ($this->upload->do_upload('filefoto')) {
$gbr = $this->upload->data();
//Compress Image
$config['image_library'] = 'gd2';
$config['source_image'] = './assets/avatar-root/' . $gbr['file_name'];
$config['create_thumb'] = FALSE;
$config['maintain_ratio'] = FALSE;
$config['quality'] = '60%';
$config['new_image'] = './assets/avatar-root/' . $gbr['file_name'];
$this->load->library('image_lib', $config);
$this->image_lib->resize();
$root = array(
'nom_admin' => strip_tags($this->input->post('nom_admin')),
'prenom_admin' => strip_tags($this->input->post('prenom_admin')),
'email_admin' => strip_tags($this->input->post('email_admin')),
'date_nais_admin' => strip_tags($this->input->post('date_nais_admin')),
'avatar_root' => strip_tags($this->input->post('avatar_root')),
'password_root' => password_hash($this->input->post('password'), PASSWORD_DEFAULT)
);
// i have a feeling you are checking if an email already exists here
// you can use form validations is_unique function in the rules above and forgo this
if ($this->M_root->email_check($root['email_admin'])) {
$this->M_root->isertion_root($root);
$this->session->set_flashdata('success_msg', 'Registered successfully.Now login to your account.');
redirect('root/login_view');
} else {
$this->session->set_flashdata('error_msg', 'bad email');
}
} else {
$this->session->set_flashdata('error_msg', $this->upload->display_errors());
}
}
}
$this->load->view("register.php");
}
You just have to change your form action to be blank so it submits to the same page.
Also in each input value except passwords you should do something like this:
<input type="text" name="username" value="<?php echo set_value('username'); ?>" size="50" />
This way the user won't lose their input when natural errors occur due to them entering wrong information or whatever.

Data retrieval in CodeIgniter Form

I have successfully created a registration and login system in Code Igniter.
After registration all the user data is saved in database.
I have used email and password in the login form and I want to display all the details related to that logged user.
I have tried a few things but not getting the exact one. How should I proceed?
//Controller file
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class User extends CI_Controller{
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
$this->load->library(array('session', 'form_validation', 'email', 'upload'));
$this->load->database();
$this->load->model('user_model');
}
public function index()
{
$this->load->view('registration');
}
public function registration()
{
//validate input value with form validation class of codeigniter
$this->form_validation->set_rules('fname', 'First Name', 'required|regex_match[/^[A-Za-z]{2,15}+$/]');
$this->form_validation->set_rules('lname', 'Last Name', 'required|regex_match[/^[A-Za-z]{2,15}+$/]');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique[user.email]');
$this->form_validation->set_rules('phone', 'Phone Number', 'required|regex_match[/^[789]\d{9}$/]');
$this->form_validation->set_rules('city', 'City', 'required|regex_match[/^[A-Za-z]{3,15}+$/]');
$this->form_validation->set_rules('zip', 'Zip Code', 'required|regex_match[/^\d{6}$/]');
$this->form_validation->set_rules('gender', 'Gender', 'required');
$this->form_validation->set_rules('password', 'Password', 'required|min_length[6]|max_length[15]');
$this->form_validation->set_rules('confirmpswd', 'Password Confirmation', 'required|matches[password]');
$this->form_validation->set_rules('image', 'Image', 'callback_do_upload','required');
//$this->form_validation->set_message('is_unique', 'This %s is already exits');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('registration', array('error' => ' ' ));
}
else
{
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$city = $_POST['city'];
$zip = $_POST['zip'];
$gender = $_POST['gender'];
$password = $_POST['password'];
$passhash = hash('md5', $password);
$image = $_FILES['image']['name'];
//call method for uploading image
$upload = $this->do_upload('image');
//md5 hashing algorithm to decode and encode input password
//$salt = uniqid(rand(10,10000000),true);
$saltid = md5($email);
$data = array('fname' => $fname,
'lname' => $lname,
'email' => $email,
'phone' => $phone,
'city' => $city,
'zip' => $zip,
'gender' => $gender,
'password' => $passhash,
'image' => $image);
if($this->user_model->insertuser($data))
{
$this->session->set_flashdata('msg','<div class="alert alert-danger text-center"> Registration Successful</div>');
redirect(base_url().'user/login');
}
else
{
$this->session->set_flashdata('msg','<div class="alert alert-danger text-center">
Something Wrong. Please try again ...</div>');
redirect(base_url());
}
}
}
public function do_upload($data)
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$this->load->library('upload', $config);
/*$config['max_size'] = 1000000;
$config['max_width'] = 1024;
$config['max_height'] = 768;*/
//base_url('uploads/')
$this->upload->initialize($config);
if ( ! $this->upload->do_upload('image'))
{
$error = array('error' => $this->upload->display_errors());
}
else
{
$data = array('upload_data' => $this->upload->data());
}
return $data;
}
public function login()
{
$this->load->view('login');
}
public function check_login()
{
$email = $_POST['email'];
$pass = hash('md5', $_POST['password']);
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
$this->form_validation->set_rules('password', 'Password', 'required|min_length[6]|max_length[15]');
if($this->form_validation->run() == FALSE)
{
$this->load->view('login');
}
else
{
$res = $this->user_model->check_user($email , $pass);
if(!empty($res))
{
echo "you are registered";
echo "<br>";
echo "<br>";
echo "your details are as follows: ";
echo "<br>";
echo "<br>";
echo "email:".$email;
echo "<br>";
}
else
{
$this->session->set_flashdata('msg','<div class="alert alert-danger text-center">email/password not found</div>');
redirect(base_url().'user/login');
}
}
}
function setSession($userId,$userName) {
$userSession = array('userId'=>$userId,
'userName'=>$userName,
'loggedIn'=>TRUE );
$this->session->set_userdata($userSession);
}
public function logout()
{
$this->session->sess_destroy();
redirect(base_url().'user/login', 'refresh');
}
}
//View File
<!DOCTYPE html>
<html lang="en">
<head>
<title>Registration</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<style type="text/css">
.form-box {
max-width: 500px;
position: relative;
margin: 5% auto;
}
</style>
</head>
<body>
<div class="wrapper">
<div class="container">
<div class="row">
<div class="form-box">
<div class="panel panel-primary">
<div class="panel-heading text-center">
<h3>Register</h3>
</div>
<?php
echo $this->session->flashdata('msg');
?>
<div class="panel-body">
<div class="row">
<div class="col-sm-12">
</div>
</div>
<form action="<?php echo base_url('user/registration');?>" method="post" enctype="multipart/form-data">
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<label class="control-label" for="fname">First Name</label>
<div>
<input type="text" class="form-control" id="fname" name="fname" placeholder="First Name" required="">
<span class="text-danger"><?php echo form_error('fname'); ?></span>
</div>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label class="control-label" for="fname">Last Name</label>
<div>
<input type="text" class="form-control" id="lname" name="lname" placeholder="Last Name" required="">
<span class="text-danger"><?php echo form_error('lname'); ?></span>
</div>
</div>
</div>
<div class="col-sm-12">
<div class="form-group">
<label class="control-label" for="email"> Email</label>
<div>
<input type="email" class="form-control" id="email" name="email" placeholder="Email" required="">
<span class="text-danger"><?php echo form_error('email'); ?></span>
</div>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label class="control-label" for="phone">Phone Number</label>
<div>
<input type="text" class="form-control" id="phone" name="phone" maxlength="10" placeholder="Phone Number" required="">
<span class="text-danger"><?php echo form_error('phone'); ?></span>
</div>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label class="control-label" for="city">City</label>
<div>
<input type="text" class="form-control" id="city" name="city" placeholder="City" required="">
<span class="text-danger"><?php echo form_error('city'); ?></span>
</div>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label class="control-label" for="zip">Zip Code</label>
<div>
<input type="text" class="form-control" id="zip" name="zip" placeholder="Zip Code" required="">
<span class="text-danger"><?php echo form_error('zip'); ?></span>
</div>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label class="control-label" for="gender">Gender</label>
<div>
<input type="radio" name="gender" value="male" id="gender" checked="true"> Male
<input type="radio" name="gender" value="female" id="gender"> Female
<input type="radio" name="gender" value="transgender" id="gender"> Transgender
<span class="text-danger"><?php echo form_error('gender'); ?></span>
</div>
</div>
</div>
<br>
<div class="col-sm-12">
<div class="form-group">
<label class="control-label" for="pswd">Password</label>
<div>
<input type="password" class="form-control" id="pswd" name="password" placeholder="Password" required="">
<span class="text-danger"><?php echo form_error('password'); ?></span>
</div>
</div>
</div>
<div class="col-sm-12">
<div class="form-group">
<label class="control-label" for="cn-pswd">Confirm Password</label>
<div>
<input type="password" class="form-control" id="cn-pswd" name="confirmpswd" placeholder="Confirm Password" required="">
<span class="text-danger"><?php echo form_error('confirmpswd'); ?></span>
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-sm-offset-5 col-sm-3 btn-submit">
<button type="submit" class="btn btn-success">Register</button>
</div>
</div>
</div>
<div class="col-sm-12">
<div class="form-group">
Select image to upload:<br>
<input type="file" name="image" id="image">
<br/><br/>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</body>
</html>
// Model file
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class User_model extends CI_Model
{
public function insertuser($data)
{
return $this->db->insert('candidates', $data);
}
public function verifyemail($key)
{
$this->db->where('md5(email)', $key);
return $this->db->update('candidates', $data);
}
public function check_user($email,$pass)
{
$sql = "SELECT id , fname FROM candidates where email = ? and password = ?";
$data = $this->db->query($sql, array($email,$pass));
return ($data->result_array()) ;
}
// Function To Fetch Selected Record
public function show_user_id($data){
$this->db->select('*');
$this->db->from('candidates');
$this->db->where('email', $data);
$q=$this->db->get('candidates');
return $q->row_array();
}
// Update Query For Selected Student
public function update_user_id1($id,$data){
$this->db->where('email', $id);
$this->db->update('candidates', $data);
}
}
?>
// While registering time your are using md5 encryption for password but while
// retrieving time you are passing password without encryption formate
$data = $this->db->query($sql, array($email,$pass));
// Use this in your model
$data = $this->db->query($sql, array($email,hash('md5', $pass)));

After uploading picture only shows blank white screen

http://127.0.0.1/masterlinkci2/admin/cpages/addpicture/33
After uploading picture 1.jpg and press upload file only a blank white screen appears I wonder why?
controllers/Cpages.php
public function addpicture() {
$gallery_id = $this->uri->segment(3);
$data['images'] = $this->Mpages->call_slideshow($gallery_id);
$this->load->view('addpicture', $data);
}
views/addpicture.php
<label><b>UPLOAD PICTURE</b></label><br><br>
<div style="margin-left: 35px">
<fieldset>
<div class="form-group">
<div class="row">
<div class="col-md-12">
<label for="filename" class="control-label">Select File to Upload</label>
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-md-12">
<input type="file" name="filename" size="20" />
<span class="text-danger"><?php if (isset($error)) { echo $error; } ?></span>
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-md-12">
<input type="submit" class="edit" value="Upload File" class="btn btn-primary"/>
</div>
</div>
</div>
</fieldset>
</div>
controllers/Cuploadfile.php
//file upload function
function uploadgallerypic()
{
//set preferences
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'jpg|gif';
$config['max_size'] = '1000000';
//load upload class library
$this->load->library('upload', $config);
$this->load->model('Mpages');
if (!$this->upload->do_upload('filename'))
{
// case - failure
$upload_error = array('error' => $this->upload->display_errors());
$this->load->view('addpicture', $upload_error);
}
else
{
// case - success
$gallery_id = $this->uri->segment(3);
$upload_data = $this->upload->data();
$filename = $upload_data['file_name'];
$this->Mpages->add_picture_gallery($filename, $gallery_id);
$data['success_msg'] = '<div class="alert alert-success text-center">Your file <strong>' . $upload_data['file_name'] . '</strong> was successfully uploaded!</div>';
$this->load->view('addpicture', $data);
}
}
Can anyone help me figure it out why after pressing "Upload File" a blank white screen appears on the screen ?

How to upload Multiple files with multiple inputs Using CodeIgniter

plaese help me dear,
I have a form where i want to upload files with multiple inputs(employee_name,resume,offer_letter,joining_letter)
using codeigniter.
My form looks like:
<form class="form-horizontal form-bordered" action="<?php echo base_url('save_employee/');?>" enctype="multipart/form-data" method="POST">
<div class="form-group">
<label for="exampleInputEmail1">Employee Image</label>
<input type="file" class="form-control" name="employee_name">
</div>
<div class="form-group">
<label for="exampleInputEmail1">Resume/CV</label>
<input type="file" class="form-control" name="resume">
</div>
<div class="form-group">
<label for="exampleInputEmail1">Offer letter</label>
<input type="file" class="form-control" name="offer_letter">
</div>
<div class="form-group">
<label for="exampleInputEmail1">Joining Letter</label>
<input type="file" class="form-control" name="joining_letter">
</div>
</form
Put This In Your Controller Here Taking Four fields To Upload The Files
public function Add() {
$data = array();
$config = array(
'upload_path' => 'upload',
'allowed_types' => 'gif|jpg|png',
'max_size' => 250,
'max_width' => 1920,
'max_heigh' => 1080,
);
$this->load->library('upload', $config);
if($this->input->post('Submit')) {
$file_data = $this->upload->data();
$data['cnic_img'] = $file_data['file_name'];
$file_data = $this->upload->data();
$data['domcie_img'] = $file_data['file_name'];
$file_data = $this->upload->data();
$data['profile_img'] = $file_data['file_name'];
$file_data = $this->upload->data();
$data['report_img'] = $file_data['file_name'];
$lat_id = $this->your_model->save($data);
}
$this->load->View('your_template');
}
Put This In You Model
function save($data){
$this->db->insert('your_table', $data);
return $this->db->insert_id();
}
Here Is Your View
<div class="portlet-body form">
<!-- BEGIN FORM-->
<?php echo $this->upload->display_errors(''); ?>
<form class="form-horizontal" action="" <?php echo form_open_multipart();?>
<div class="form-group">
<label for="CNIC Image" class="col-md-3 control-label">CNIC Image:</label>
<div class="col-md-9">
<input type="file" id="cnic_img" name="cnic_img">
<p class="help-block">
Upload CNIC image Here.
</p>
</div>
</div>
</div>
</div>
<div class="portlet-body form">
<div class="form-body">
<div class="form-group">
<label for="Domicile Image" class="col-md-3 control-label">Domicile Image:</label>
<div class="col-md-9">
<input type="file" id="domcie_img" name="domcie_img">
<p class="help-block">
Upload Domicile Image Here.
</p>
</div>
</div>
</div>
</div>
<div class="portlet-body form">
<div class="form-body">
<div class="form-group">
<label for="Domicile Image" class="col-md-3 control-label">Profile Image:</label>
<div class="col-md-9">
<input type="file" id="profile_img" name="profile_img" >
<p class="help-block">
Upload Profile Image Here.
</p>
</div>
</div>
</div>
</div>
<div class="portlet-body form">
<div class="form-body">
<div class="form-group">
<label for="Domicile Image" class="col-md-3 control-label">Police validation Report:</label>
<div class="col-md-9">
<input type="file" id="report_img" name="report_img">
<p class="help-block">
Upload Police Validation Report Here.
</p>
</div>
</div>
</div>
</div>
This example is helped for some peoples.
$file = $_FILES;
$config['upload_path'] = './images/product/';
$config['allowed_types'] = 'jpg|jpeg';
$config['max_size'] = 2000;
$config['max_width'] = 1500;
$config['max_height'] = 1500;
$config['encrypt_name'] = TRUE;
if (empty($file['product_image']['name'])) {
$this->form_validation->set_rules('product_image', 'Product image', 'required');
} else {
// Load and initialize upload library
$this->load->library('upload', $config);
$this->upload->initialize($config);
if($this->upload->do_upload('product_image')){
$fileData = $this->upload->data();
} else {
$web['error']['product_image'] = $this->upload->display_errors();
$this->load->view('web_template',$web);
}
}
if (empty($file['customer_image']['name'])) {
$this->form_validation->set_rules('customer_image', 'Customer image', 'required');
} else {
// Load and initialize upload library
$this->load->library('upload', $config);
$this->upload->initialize($config);
if($this->upload->do_upload('customer_image')){
$cusfileData = $this->upload->data();
} else {
$web['error']['customer_image'] = $this->upload->display_errors();
$this->load->view('web_template',$web);
}
}
Get the image file name to insert data
$data = array(
'product_image' => $fileData['file_name'],
'customer_image' => $cusfileData['file_name']
);

Resources