This is my Codeigniter Controller and Model for user image and data upload.
I have some questions here:
- Is this good way of writing this script?
- Can this code be put to production if its need is urgent?
Controller
<?php
class Traveller extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->helper('url');
$this->load->helper('form');
$this->load->library('form_validation');
$this->load->library('upload');
$this->load->database();
$this->load->model('Country_states_cities');
$this->load->model("register_modal");
$this->load->model("Couch_reg");
}
public function traveller_registration(){
//Validations
$rules=array (
"first_name"=> array(
"field"=>"first_name",
"label"=>"first Name",
"rules"=>"required"
),
"last_name"=> array(
"field"=>"last_name",
"label"=>"Last Name",
"rules"=>"required"
),
"password"=> array(
"field"=>"password",
"label"=>"Password",
"rules"=>"required|max_length[20]|min_length[6]"
),
"pass_conf"=> array(
"field"=>"pass_conf",
"label"=>"Password",
"rules"=>"required|matches[password]"
),
"email"=> array(
"field"=>"email",
"label"=>"Email",
"rules"=>"required|valid_email|callback_email_is_taken"
)
); //rules array ends
//set rules
$this->form_validation->set_rules($rules);
//Override the message
$this->form_validation->set_message('required','The %s field is empty');
// check to see if form has been submitted
if ($this->form_validation->run()!=true) {
$data['countryDrop'] = $this->Country_states_cities->getCountries();
$this->load->view('header');
$this->load->view('register',$data); //Display page
$this->load->view('footer');
}else{
$form=array();
$form['first_name']=$this->input->post("first_name",true);
$form['last_name']=$this->input->post("last_name",true);
$form['dob']=date('Y-m-d',strtotime($this->input->post("dob",true)));
$form['email']=$this->input->post("email",true);
$form['password']=sha1($this->input->post("password",true));
$form['sex']=$this->input->post("sex",true);
$form['phone']=$this->input->post("phone",true);
$form['phone']=$this->input->post("activities",true);
$form['help_others']=$this->input->post("help_other",true);
$form['wish_country']=$this->input->post("wish_city",true);
$form['country']=$this->input->post("country_name",true);
$form['state']=$this->input->post("state_id",true);
$form['city']=$this->input->post("city_id",true);
$form['addline1']=$this->input->post("addressline1",true);
$form['addline2']=$this->input->post("addressline2",true);
$form['zip']=$this->input->post("zip",true);
#####################
# Uploading Images ##
#####################
$files = $_FILES;
$cpt = count($_FILES['uploadfile']['name']);
for($i=0; $i<$cpt; $i++)
{
$_FILES['uploadfile']['name']= $files['uploadfile']['name'][$i];
$_FILES['uploadfile']['type']= $files['uploadfile']['type'][$i];
$_FILES['uploadfile']['tmp_name']= $files['uploadfile']['tmp_name'][$i];
$_FILES['uploadfile']['error']= $files['uploadfile']['error'][$i];
$_FILES['uploadfile']['size']= $files['uploadfile']['size'][$i];
$this->upload->initialize($this->set_upload_options());
$this->upload->do_upload('uploadfile');
$upload_data = $this->upload->data();
$name_array[] = $upload_data['file_name']; //success till here now inserting in database and creating thumbnails
$fileName = $upload_data['file_name'];
$images[] = $fileName;
}
$fileName = $images;
var_dump($images);
@$form['picture1']=$images[0];
@$form['picture2']=$images[1];
@$form['picture3']=$images[2];
@$form['picture4']=$images[3];
@$form['picture5']=$images[4];
// $result = $this->Couch_reg->insert_new_user($form);
$this->load->view("test");
echo '<pre>';
// print_r($_FILES);
echo '</pre>';
echo '<pre>';
}
}//function index ends
public function email_is_taken($input) {
$query="SELECT * FROM `tbl_usrs` WHERE `email`=? ";
$arg=array ($input);
$exec=$this->db->query($query,$arg) or die(mysqli_error());
if($exec->num_rows() >0)
{
$this->form_validation->set_message('email_is_taken','Sorry the email <b> '.$input.' </b> is already taken');
return false;
}else {
return true;
}
}
public function get_countries() {
$result=$this->register_modal->getCountries();
echo $result;
}
public function register_me_as() {
$this->load->helper('url');
$this->load->view('header');
$this->load->view('register_me_as'); //Display page
// $this->load->view('footer');
}
private function set_upload_options()
{
// upload an image options
$config = array();
$config['upload_path'] = LARGEPATH; //give the path to upload the image in folder
$config['remove_spaces']=TRUE;
$config['encrypt_name'] = TRUE; // for encrypting the name
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '78000';
$config['overwrite'] = FALSE;
return $config;
}
}//class ends
Model
<?php
class Couch_reg extends CI_Model{
//insert new post
function __construct() {
parent::__construct();
$this->load->database();
}
function insert_new_user($form)
{
$this->db->insert('tbl_usrs',$form);
if ($this->db->affected_rows() > 0) {
return true;
} else {
return false;
}
}
}