How to upload picture using codeigniter? - php

I can manage to insert data into the tblaccount. But the problem is the picture can't be uploaded. The tblaccount contains the right data of firstname, lastname, email, department, username, and password, but the picture remains blank even I've uploaded some images.
UPDATE: The picture can now be uploaded in the folder, but no luck in tblaccount. It only show blank data.
signup.php controller:
public function index()
{
// set form validation rules
$this->form_validation->set_rules('firstname', 'First Name', 'trim|required|alpha|min_length[3]|max_length[30]');
$this->form_validation->set_rules('lastname', 'Last Name', 'trim|required|alpha|min_length[3]|max_length[30]');
$this->form_validation->set_rules('email', 'Email Address', 'trim|required|valid_email|is_unique[tblaccount.Email]');
$this->form_validation->set_rules('department', 'Department', 'trim|required|alpha|min_length[3]|max_length[30]');
$this->form_validation->set_rules('username', 'Username', 'trim|required|alpha|min_length[3]|max_length[30]|is_unique[tblaccount.Username]');
$this->form_validation->set_rules('password', 'Password', 'trim|required');
$this->form_validation->set_rules('cpassword', 'Confirm Password', 'trim|required|matches[password]');
$this->form_validation->set_rules('picture', 'Image', 'trim|required');
// submit
if ($this->form_validation->run() == FALSE)
{
// fails
$this->load->view('signup_view');
}
else
{
if(!empty($_FILES['picture']['name']))
{
$config['upload_path'] = './uploads/images/';
$config['allowed_types'] = 'jpg|jpeg|png|gif';
$config['max_size'] = 10000000;
$config['file_name'] = $_FILES['picture']['name'];
//Load upload library and initialize configuration
$this->load->library('upload',$config);
$this->upload->initialize($config);
if($this->upload->do_upload('picture'))
{
$uploadData = $this->upload->data();
$picture = $uploadData['file_name'];
}
else
{
$picture = '';
$error = array('error' => $this->upload->display_errors());
echo "<script>alert('JPG, JPEG, PNG and GIF type of file only is allowed and atleast 10MB of size');window.location = '".base_url("index.php/signup")."';</script>";
}
}
else
{
$picture = '';
}
$data = array(
'First_Name' => $this->input->post('firstname'),
'Last_Name' => $this->input->post('lastname'),
'Email' => $this->input->post('email'),
'Department' => $this->input->post('department'),
'Username' => $this->input->post('username'),
'Password' => $this->input->post('password'),
'Picture' => $picture
);
if ($this->account_model->insert($data))
{
$this->session->set_flashdata('msg','<div class="alert alert-success text-center">You are successfully registered! Please login to access your profile!</div>');
redirect('login');
}
else
{
// error
$this->session->set_flashdata('msg','<div class="alert alert-danger text-center">Oops! Error. Please try again later!!!</div>');
redirect('signup');
}
}
}
signup.php view:
<div class="row">
<div class="col-md-4 col-md-offset-4 well">
<?php echo form_open_multipart('signup');?>
<legend>Signup</legend>
<div class="form-group">
<label for="name">First Name</label>
<input class="form-control" name="firstname" placeholder="First Name" type="text" value="<?php echo set_value('First_Name');?>"/>
<span class="text-danger"><?php echo form_error('firstname'); ?></span>
</div>
<div class="form-group">
<label for="name">Last Name</label>
<input class="form-control" name="lastname" placeholder="Last Name" type="text" value="<?php echo set_value('Last_Name');?>"/>
<span class="text-danger"><?php echo form_error('lastname'); ?></span>
</div>
<div class="form-group">
<label for="email">Email Address</label>
<input class="form-control" name="email" placeholder="Email Address" type="text" value="<?php echo set_value('Email');?>"/>
<span class="text-danger"><?php echo form_error('email'); ?></span>
</div>
<div class="form-group">
<label for="email">Department</label>
<input class="form-control" name="department" placeholder="Department" type="text" value="<?php echo set_value('Department');?>"/>
<span class="text-danger"><?php echo form_error('department'); ?></span>
</div>
<div class="form-group">
<label for="email">Username</label>
<input class="form-control" name="username" placeholder="Username" type="text" value="<?php echo set_value('Username');?>"/>
<span class="text-danger"><?php echo form_error('username'); ?></span>
</div>
<div class="form-group">
<label for="subject">Password</label>
<input class="form-control" name="password" placeholder="Password" type="password"/>
<span class="text-danger"><?php echo form_error('password'); ?></span>
</div>
<div class="form-group">
<label for="subject">Confirm Password</label>
<input class="form-control" name="cpassword" placeholder="Confirm Password" type="password"/>
<span class="text-danger"><?php echo form_error('cpassword'); ?></span>
</div>
<div class="form-group">
<label for="subject">Profile Picture:</label>
<input class="form-control" name="picture" accept="image/*" type="file"/>
<span class="text-danger"><?php echo form_error('picture'); ?></span>
</div>
<div class="form-group">
<button name="submit" type="submit" class="btn btn-info">Signup</button>
<button name="cancel" type="reset" class="btn btn-info">Cancel</button>
</div>
<?php echo form_close(); ?>
</div>
</div>

Remove required from validation check as it is not allowed to pass in image field ,when you check empty file field (!empty($_FILES['picture']['name']) then required validation already checked inside it , Secondly you have to check whether the directory is created or not and provide 777 permission to it . I have tested code by adding only these two check. Hope it help
$this->form_validation->set_rules('picture', 'Image', 'trim'); /*Change in this line -- remove required*/
if ($this->form_validation->run()) {
if (!empty($_FILES['picture']['name'])) {
$config['upload_path'] = 'uploads/images/';
/*add 777 permission to directory*/
if (!is_dir($config['upload_path'])) {
mkdir($config['upload_path'], 0777, TRUE);
}
$config['allowed_types'] = 'jpg|jpeg|png|gif';
$config['max_size'] = 10000000;
$config['file_name'] = $_FILES['picture']['name'];
//Load upload library and initialize configuration
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ($this->upload->do_upload('picture')) {
$uploadData = $this->upload->data();
$picture = $uploadData['file_name'];
} else {
$picture = '';
$error = array('error' => $this->upload->display_errors());
echo "<script>alert('JPG, JPEG, PNG and GIF type of file only is allowed and atleast 10MB of size');window.location = '" . base_url("index.php/signup") . "';</script>";
}
} else {
$picture = '';
}
$data = array(
'image' => $picture
);
$this->write_conn->db->insert('test', $data);
if ($this->write_conn->db->insert('test', $data)) {
$this->session->set_flashdata('msg', '<div class="alert alert-success text-center">You are successfully registered! Please login to access your profile!</div>');
} else {
// error
$this->session->set_flashdata('msg', '<div class="alert alert-danger text-center">Oops! Error. Please try again later!!!</div>');
}
}

On your upload path
$config['upload_path'] = 'uploads/images/';
Try
$config['upload_path'] = './uploads/images/';
$config['upload_path'] = FCPATH . '/uploads/images/';
And I use form_open_multipart() form helper make sure your folder correct permissions
On another note make sure you have named your file and class name correct where signup.php would be Signup.php Only the first letter should be uppercase on class and filename explained here https://www.codeigniter.com/user_guide/general/styleguide.html#file-naming
<?php
class Signup extends CI_Controller {
}

Related

Image uploading in codeigniter in database

I have a problem with uploading images in the database in codeigniter. When I upload image in the database, it shows this type of error like:
The upload path does not appear to be valid. So what's the problem in this code. I cannot figure out, so please help me.
I tried base_url() method also but error is not solved...!
My Htmlcode:-
<?php echo form_open_multipart('student/insertstudent') ?>
<form>
<div class="panel panel-primary" >
<div class="panel-heading" >
<p class="label" style="font-size: 15px">Student Registration Form</p>
</div>
<div class="panel-body">
<fieldset>
<div class="form-group">
<label for="exampleInputEmail1">First Name</label>
<input type="text" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter First Name" name="fname" value="<?php echo set_value('fname'); ?>">
<div class="row-lg-6">
<?php echo form_error('fname'); ?>
</div>
</div>
<div class="form-group">
<label for="exampleInputPassword1">Last Name</label>
<input type="text" class="form-control" id="exampleInputPassword1" placeholder="Enter Last Name" name="lname" value="<?php echo set_value('lname'); ?>">
<div class="row-lg-6">
<?php echo form_error('lname'); ?>
</div>
</div>
<div class="form-group">
<label for="exampleTextarea">Address</label>
<textarea class="form-control" id="exampleTextarea" rows="2" placeholder="Enter Address" name="add"><?php echo set_value('add'); ?></textarea>
<div class="row-lg-6">
<?php echo form_error('add'); ?>
</div>
</div>
<div class="form-group">
<label for="exampleInputPassword1">Contact Number</label>
<input type="text" class="form-control" id="exampleInputPassword1" placeholder="Enter Contact Number" name="cno" value="<?php echo set_value('cno'); ?>">
<div class="row-lg-6">
<?php echo form_error('cno'); ?>
</div>
</div>
<div class="form-group">
<label for="exampleInputPassword1">Emaid ID</label>
<input type="text" class="form-control" id="exampleInputPassword1" placeholder="Enter your email" name="email" value="<?php echo set_value('email'); ?>">
<div class="row-lg-6">
<?php echo form_error('email'); ?>
</div>
</div>
<div class="form-group">
<label for="exampleInputPassword1">Enter Course</label>
<input type="text" class="form-control" id="exampleInputPassword1" placeholder="Enter Course Name" name="cname" value="<?php echo set_value('cname'); ?>">
<div class="row-lg-6">
<?php echo form_error('cname'); ?>
</div>
</div>
<!-- <div class="form-group">
<label for="exampleInputPassword1">Select Student Photo</label>
<input type="file" class="form-control" id="exampleInputPassword1" placeholder="Enter Course Name" name="photo">
<div class="row-lg-6">
<?php echo form_error('photo'); ?>
</div>
</div> -->
<div class="form-group">
<label for="exampleInputPassword1">Select Image</label>
<?php echo form_upload(['name' => 'userfile','class'=>'form-control']); ?>
<div class="row-lg-6">
<?php if(isset($upload_error))
{
echo $upload_error;
} ?>
</div>
</div>
<h4 align="center" style="margin-left: -150px"><button type="submit" class="btn btn-primary">Register</button></h4>
<h4 align="center" style="margin-top: -43px;margin-left: 200px"><button type="reset" class="btn btn-primary">Reset</button></h4>
</fieldset>
</div>
</div>
my controller file:-
public function insertstudent()
{
$this->form_validation->set_rules('fname', 'FirstName', 'required|alpha');
$this->form_validation->set_rules('lname', 'lastName', 'required|alpha');
$this->form_validation->set_rules('add', 'Address', 'required');
$this->form_validation->set_rules('cno', 'Contact Number', 'required|numeric');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
$this->form_validation->set_rules('cname', 'Course Name', 'required');
// $this->form_validation->set_rules('userfile', 'Student Photo', 'required');
$this->form_validation->set_error_delimiters("<p class='text-danger'>", "</p>");
$config['upload_path'] = base_url('asset/uploads/');
$config['allowed_types'] = 'gif|jpg|png';
$this->upload->initialize($config);
$this->load->library('upload',$config);
if ($this->form_validation->run() == TRUE && $this->upload->do_upload('userfile')) {
$post = $this->input->post();
$data = $this->upload->data();
$image_path = base_url("upload/".$data['raw_name'].$data['file_ext']);
$post['image_path'] = $image_path;
$this->load->model('useradd');
if($this->useradd->addstudent($post)){
$this->session->set_flashdata('success', 'Record Successfully Inserted');
return redirect('admin');
}else {
$this->load->view('addstudent');
}
// $data = array(
// 'firstname' => $this->input->post('fname'),
// 'lastname' => $this->input->post('lname'),
// 'address' => $this->input->post('add'),
// 'phone' => $this->input->post('cno'),
// 'email' => $this->input->post('email'),
// 'course' => $this->input->post('cname'),
// 'photo' => $image_path,
// );
// // $this->load->library('upload', $data);
// if ($result == true) {
// $this->session->set_flashdata('success', 'Record Successfully Inserted');
// return redirect('admin');
// } else {
// $this->load->view('addstudent');
// }
} else {
$upload_error = $this->upload->display_errors();
$this->load->view('addstudent',compact('upload_error'));
}
}
my Model:-
class useradd extends CI_Model
{
function addstudent($data)
{
$result = $this->db->insert('add_student', $data);
}
}
My error is:
The upload path does not appear to be valid.
Try This Code To Upload Image.and remove form tag in your view file and you didn't define form_close method at the end of the form of your view file
$file = $_FILES['filename']['name'];
$config['upload_path'] = './images/';
$config['allowed_types'] = 'gif|jpg|png';
$this->load->library('upload', $config);
$this->upload->do_upload();
$allData = $this->upload->data();

Entering wrong captcha and clicking on submit button clearing all the form field values in codeigniter php

Hi i have request form if i enter wrong captcha and click on submit button all the form fields which i have entered are getting cleared.If there is any error in the captcha form only the captcha has to be refreshed and the form values should not be cleared.
View:
<form name="contact" id="contactform" enctype="multipart/form-data" method="post" action="<?php echo base_url();?>welcome/request">
<div class="name"><span class="mandatory"></span>
<input type="text" class="form-control" name="name" id="name" placeholder="Name" value="<?php echo set_value('name');?>" required>
<?php echo form_error('name', '<div class="error">', '</div>'); ?>
</div>
<div class="name">
<input type="text" class="form-control" id="phone" name="phone" value="<?php echo set_value('phone');?>" placeholder="Phone" MinLength ="10" MaxLength="10" onkeypress="return isNumber(event)" required>
<?php echo form_error('phone', '<div class="error">', '</div>'); ?>
</div>
<div class="email">
<input type="email" class="form-control" id="email" name="email" placeholder="Email" required value="<?php echo set_value('email');?>"/>
<?php echo form_error('email', '<div class="error">', '</div>'); ?>
</div>
<div class="description">
<textarea name="description" style="width:100%;overflow:auto;" id="description" placeholder="Description" maxlength="3000" required value="<?php echo set_value('description');?>"/></textarea>
</div>
<p class="entertext">Enter Image Text</p>
<div class="">
<div class="captchas">
<div class="captchatexts" ><?php echo $captchaImg;?></div>
<p id = 'ref_symbol' class='refresh clickto'>Click to change</p>
</div>
<div class="captcha">
<input type="text" class="form-control" id="captcha" name="captcha" style="background-color: #EAD5D2;border: none;" required>
<?php echo form_error('captcha', '<div class="error">', '</div>'); ?>
</div>
</div>
<input type="hidden" value="true" name="isSubmitted">
<button type="submit" id="submit" class="btn btn-success" onclick="return validate();" >Submit</button><!-- id="demo"!-->
</form>
Controller:
function request()
{
$this->load->library('form_validation');
if ($this->input->post('email'))
{
$this->form_validation->set_error_delimiters('<br /><span class="error"> ','</span>');
$this->form_validation->set_rules('name','First Name' , 'required');
$this->form_validation->set_rules('email','Email','required|valid_email');
$this->form_validation->set_rules('phone','Mobile Number','required|numeric');
$this->form_validation->set_rules('description','Description','required');
$this->form_validation->set_rules('captcha', 'Captcha', 'required');
if($this->form_validation->run()== FALSE)
{
/*echo "valid";
print_r($this->input->post());
exit;*/
$data['mainpage']='index';
$this->load->view('templates/template',$data);
}
else
{
/*echo "invalid";
print_r($this->input->post());
exit;*/
$inputCaptcha = $this->input->post('captcha');
$sessCaptcha = $this->session->userdata('captchaCode');
if ($inputCaptcha === $sessCaptcha)
{
$result = $this->index_model->send_mail($this->input->post('email'));
if ($result)
{
$this->flash->success('<h2 style="color:green">Thank You! Your Message has been sent</h2>');
redirect('welcome');
}
else
{
$this->flash->success('<h2 style="color:red">Sorry ! Message sending failed</h2>');
redirect('welcome');
}
}
else
{
$this->flash->success('<h2 style="color:red;font-size:15px;margin-top:6px;">Please enter correct captcha.</h2>');
redirect('welcome');
}
}
}
$config = array(
'img_path' => 'captcha_images/',
'img_url' => base_url() . 'captcha_images/',
'img_width' => '125',
'img_height' => 35,
'word_length' => 6,
'font_size' => 30
);
$captcha = create_captcha($config);
$word = $captcha['word'];
$this->session->unset_userdata('captchaCode');
$this->session->set_userdata('captchaCode', $word);
$data['captchaImg'] = $captcha['image'];
//$data['captchaImg'] = $captcha['image'];
$data['mainpage'] = "index";
$this->load->view('templates/template',$data);
}
Can anyone please help me out this
Don't load view twice, try to remove the loading of view from if($this->form_validation->run()== FALSE) because when the view loads then set_value inside it is triggered and you have called loading view after creating captcha. So remove the load view when validation errors are there like,
....
if($this->form_validation->run()== FALSE) {
/*echo "valid";
print_r($this->input->post());
exit;*/
$data['mainpage']='index';
// comment the below line, otherwise there are no loophole in your code
//$this->load->view('templates/template',$data);
}
....
Hope the above will work for you.

Fatal error: Call to undefined function form_open() line 104

I know most of you guys will realize this is a dumb question but I don't have any idea what's wrong with my code. I'm trying to create a contact form that will be sent into the database. But when I try to go to the contact_form_view.php it says Fatal error: Call to undefined function form_open() on Line 104
Here's the form code
<?php $attributes = array("name" => "contactform");
echo form_open("contactform/index", $attributes);?>
<div class="form-group">
<label for="name">Name</label>
<input class="form-control" name="name" placeholder="Your Full Name" type="text" value="<?php echo set_value('name'); ?>" />
<span class="text-danger"><?php echo form_error('name'); ?></span>
</div>
<div class="form-group">
<label for="email">Email ID</label>
<input class="form-control" name="email" placeholder="Email-ID" type="text" value="<?php echo set_value('email'); ?>" />
<span class="text-danger"><?php echo form_error('email'); ?></span>
</div>
<div class="form-group">
<label for="subject">Subject</label>
<input class="form-control" name="subject" placeholder="Subject" type="text" value="<?php echo set_value('subject'); ?>" />
<span class="text-danger"><?php echo form_error('subject'); ?></span>
</div>
<div class="form-group">
<label for="message">Message</label>
<textarea class="form-control" name="message" rows="4" placeholder="Message"><?php echo set_value('message'); ?></textarea>
<span class="text-danger"><?php echo form_error('message'); ?></span>
</div>
<div class="form-group">
<button name="submit" type="submit" class="btn btn-success">Submit</button>
</div>
<?php echo form_close(); ?>
<?php echo $this->session->flashdata('msg'); ?>
Here's the php code
<?php
class contactform extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper(array('form','url'));
$this->load->library(array('session', 'form_validation'));
$this->load->database();
}
function index()
{
//set validation rules
$this->form_validation->set_rules('name', 'Name', 'trim|required|xss_clean|callback_alpha_space_only');
$this->form_validation->set_rules('email', 'Emaid ID', 'trim|required|valid_email');
$this->form_validation->set_rules('subject', 'Subject', 'trim|required|xss_clean');
$this->form_validation->set_rules('message', 'Message', 'trim|required|xss_clean');
//run validation on post data
if ($this->form_validation->run() == FALSE)
{ //validation fails
$this->load->view('contact_form_view');
}
else
{
//insert the contact form data into database
$data = array(
'name' => $this->input->post('name'),
'email' => $this->input->post('email'),
'subject' => $this->input->post('subject'),
'message' => $this->input->post('message')
);
if ($this->db->insert('contacts', $data))
{
// success
$this->session->set_flashdata('msg','<div class="alert alert-success text-center">We received your message! Will get back to you shortly!!!</div>');
redirect('contactform/index');
}
else
{
// error
$this->session->set_flashdata('msg','<div class="alert alert-danger text-center">Oops! Some Error. Please try again later!!!</div>');
redirect('contactform/index');
}
}
}
//custom callback to accept only alphabets and space input
function alpha_space_only($str)
{
if (!preg_match("/^[a-zA-Z ]+$/",$str))
{
$this->form_validation->set_message('alpha_space_only', 'The %s field must contain only alphabets and space');
return FALSE;
}
else
{
return TRUE;
}
}
}
?>
if you please try to load form helper in your autoload.php like this
$autoload['helper'] = array('url', 'form');
i think you load the helper in the controller which have the action of your form not the controller that call the form.
so just add the helper to autoload.php and it will work

Uploading in codeigniter doesn't work. what can be the reason?

This is the controller, and i have no errors
public function admin_page(){
if($this->input->post('add_product')){
if(empty($_FILES['userfile']['name'])){
$data = array(
'category' => $this->input->post('category') ,
'product_name' => $this->input->post('name') ,
'description' => $this->input->post('description'),
'image' => 'no_image.jpg'
);
} else{
$config['upload_path'] = './public/images/';
$config['allowed_types'] = 'gif|jpg|png|jpeg|JPG';
$config['max_size'] = '1000';
$this->load->library('upload', $config);
$this->upload->do_upload('userfile');
echo 'uploaded :)';
$data = array(
'category' => $this->input->post('category') ,
'product_name' => $this->input->post('name') ,
'description' => $this->input->post('description'),
'image' => $_FILES['userfile']['name']
);
}
$this->load->model('products');
$this->products->add_product($data);
}
$this->load->view('admin_page');
}
View
<form action="<?php echo base_url();?>admin/admin_page" method="post" enctype="multipart/form-data">
<div class="col-xs-3">
<div class="form-group">
<label for="exampleInputEmail1"> Product name </label>
<input type="text" name="name" class="form-control" >
</div>
<div class="form-group">
<label for="exampleInputEmail1"> Category </label>
<input type="text" name="category" class="form-control">
</div>
<div class="form-group">
<label for="exampleInputEmail1"> Description </label>
<textarea class="form-control" name="description" rows="3"></textarea>
</div>
<div class="form-group">
<label for="exampleInputEmail1"> Image </label>
<input type="file" name="userfile" class="form-control">
</div>
<input type="submit" name="add_product" class="btn btn-default" value="OK" />
</div>
</form>
</div>
Note: Things to check on codeigniter 3.
Make sure the controller and model name has first letter upper case
Admin.php and class Admin extends CI_Controller {
And model Products.php and class Products extends CI_Model {
CI2 Upload http://www.codeigniter.com/userguide2/libraries/form_validation.html
CI3 Upload http://www.codeigniter.com/user_guide/libraries/file_uploading.html
Controller
<?php
class Admin extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->load->library('upload');
//$this->load->model('products');
}
public function index() {
}
public function admin_page() {
$this->form_validation->set_rules('name', 'Name', 'required');
$this->form_validation->set_rules('category', 'Category', 'required');
$this->form_validation->set_rules('description', 'Description', 'required');
if ($this->form_validation->run() == FALSE) {
$data['upload_errors'] = $this->upload->display_errors();
$this->load->view('admin_page', $data);
} else {
/*
* If userfile checks if.
*
*/
if (!empty($_FILES['userfile']['name'])) {
/*
*
* Make sure your images upload path is on the main directory
*
* application
* public
* public > images
* system
* .htaccess
* index.php
*
*/
$config['upload_path'] = './public/images/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '3000';
$config['max_width'] = '0';
$config['max_height'] = '0';
$this->upload->initialize($config);
$field_name = 'userfile';
if ( ! $this->upload->do_upload($field_name)) {
$data['upload_errors'] = $this->upload->display_errors();
$this->load->view('admin_page', $data);
} else {
/*
* Using the variable below you are able to get codeigniter file upload info.
* Note: Codeigniter upload only made for one file
* $upload_data = $this->upload->data();
*
*/
$upload_data = $this->upload->data();
$data = array(
'category' => $this->input->post('category') ,
'product_name' => $this->input->post('name') ,
'description' => $this->input->post('description'),
'image' => $upload_data['file_name']
);
$this->products->add_product($data);
$data['upload_errors'] = '';
$this->load->view('admin_page', $data);
}
/*
If no image isset
*/
} else {
echo "No File Isset";
$data = array(
'category' => $this->input->post('category') ,
'product_name' => $this->input->post('name') ,
'description' => $this->input->post('description'),
'image' => 'no_image.jpg'
);
$this->products->add_product($data);
$data['upload_errors'] = '';
$this->load->view('admin_page', $data);
}
}
}
}
View
Changed input submit to button submit.
<div class="container">
<?php echo validation_errors('<div class="alert alert-danger">', '</div>'); ?>
<?php if ($upload_errors) {?>
<div class="alert alert-info"><?php echo $upload_errors;?></div>
<?php }?>
<?php echo form_open_multipart('admin/admin_page');?>
<div class="form-group">
<label>Product name</label>
<input type="text" name="name" class="form-control" value="<?php echo set_value('name');?>" size="50">
</div>
<div class="form-group">
<label>Category</label>
<input type="text" name="category" value="<?php echo set_value('category');?>" class="form-control" size="50">
</div>
<div class="form-group">
<label>Description</label>
<textarea class="form-control" name="description" rows="3"></textarea>
</div>
<div class="form-group">
<label>Image</label>
<input type="file" name="userfile" size="20">
</div>
<div class="form-group">
<button type="submit" class="btn btn-default">Save</button>
</div>
<?php echo form_close();?>
</div>
I would use codeigniter's form_open_multipart() http://www.codeigniter.com/user_guide/helpers/form_helper.html
All working my end.

Uploading image from one function

I have a edit function on my users codeigniter project, I would like to be able to use that one function for uploading images as well but user guide says to create a new function.
How am I able to make it with same edit() function.
function edit($user_id = 0) {
$this->load->model('users/model_user');
$data['title'] = "Users";
$data['base'] = config_item('HTTP_SERVER');
$data['isLogged'] = $this->user->isLogged();
$data['last_updated'] = $this->model_user->last_updated($user_id);
$data['user_id'] = $user_id;
if (!empty($this->input->post('username'))) {
$data['username'] = $this->input->post('username');
} else {
$data['username'] = $this->model_user->getUserByUsername($user_id);
}
if (!empty($this->input->post('firstname'))) {
$data['firstname'] = $this->input->post('firstname');
} else {
$data['firstname'] = $this->model_user->getUserByFirstname($user_id);
}
if (!empty($this->input->post('lastname'))) {
$data['lastname'] = $this->input->post('lastname');
} else {
$data['lastname'] = $this->model_user->getUserByLastname($user_id);
}
if (!empty($this->input->post('email'))) {
$data['email'] = $this->input->post('email');
} else {
$data['email'] = $this->model_user->getUserByEmail($user_id);
}
// User userfile
if(!empty($this->input->post('userfile'))) {
$data['userfile'] = $this->input->post('userfile');
} else {
$data['userfile'] = $this->model_user->getUserImage($user_id);
}
$config['upload_path'] = base_url('image/catalog/'); // base_url('image/catalog/');
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1000';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
if (!$this->upload->do_upload()) {
$data['error'] = $this->upload->display_errors();
$data['header'] = $this->load->view('template/common/header', $data, TRUE);
$data['footer'] = $this->load->view('template/common/footer', NULL, TRUE);
return $this->load->view('template/users/users_form', $data);
} else {
$data['upload_data'] = $this->upload->data();
$data['header'] = $this->load->view('template/common/header', $data, TRUE);
$data['footer'] = $this->load->view('template/common/footer', NULL, TRUE);
return $this->load->view('template/users/users_form', $data);
}
$this->load->library('password');
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Your Username');
$this->form_validation->set_rules('firstname', 'Your First Name');
$this->form_validation->set_rules('lastname', 'Your Last Name');
$this->form_validation->set_rules('email', 'Your Email');
if ($this->form_validation->run() == TRUE) {
$this->model_user->editUser($user_id, $data);
redirect('users');
} else {
$data['header'] = $this->load->view('template/common/header', $data, TRUE);
$data['footer'] = $this->load->view('template/common/footer', NULL, TRUE);
return $this->load->view('template/users/users_form', $data);
}
}
View File
<?php echo validation_errors('<div class="alert alert-danger">', '</div>'); ?>
<?php echo $error ;?>
<?php
$data = array(
'role' => "form",
'class' => 'form-horizontal'
);
echo form_open('users/edit/' . $user_id, $data);
?>
<div class="form-group">
<label for="input-username" class="col-sm-2 control-label">Username</label>
<div class="col-sm-10">
<input type="text" name="username" value="<?php echo $username;?>" class="form-control">
</div>
</div>
<div class="form-group">
<label for="input-firstname" class="col-sm-2 control-label">Firstname</label>
<div class="col-sm-10">
<input type="text" name="firstname" value="<?php echo $firstname;?>" class="form-control">
</div>
</div>
<div class="form-group">
<label for="input-lastname" class="col-sm-2 control-label">Lastname</label>
<div class="col-sm-10">
<input type="text" name="lastname" value="<?php echo $lastname;?>" class="form-control">
</div>
</div>
<div class="form-group">
<label for="input-email" class="col-sm-2 control-label">Email</label>
<div class="col-sm-10">
<input type="text" name="email" value="<?php echo $email;?>" class="form-control">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label" for="input-image"></label>
<div class="col-lg-3">
<div class="thumbnail">
<img src="<?php echo $userfile;?>">
</div>
</div>
</div>
<div class="form-group">
<label for="input-image" class="col-sm-2 control-label">Image</label>
<div class="col-sm-10">
<br>
<input type="file" name="userfile" value="<?php echo $userfile;?>" size="20">
</div>
</div>
<div class="form-group">
<div class="text-right">
<h2><a href="<?php echo base_url('users/edit_password/' . $user_id);?>">Change Your Password</h2>
<button type="submit" name="submit" class="btn btn-primary">Submit</button>
</div>
</div>
<?php echo form_close();?>
I have got it to work with out using codeigniter upload image helper easier this way.
controller function
function edit($user_id = 0) {
$this->load->model('users/model_user');
$data['title'] = "Users";
$data['base'] = config_item('HTTP_SERVER');
$data['isLogged'] = $this->user->isLogged();
$data['last_updated'] = $this->model_user->last_updated($user_id);
$data['user_id'] = $user_id;
if (!empty($this->input->post('username'))) {
$data['username'] = $this->input->post('username');
} else {
$data['username'] = $this->model_user->getUserByUsername($user_id);
}
if (!empty($this->input->post('firstname'))) {
$data['firstname'] = $this->input->post('firstname');
} else {
$data['firstname'] = $this->model_user->getUserByFirstname($user_id);
}
if (!empty($this->input->post('lastname'))) {
$data['lastname'] = $this->input->post('lastname');
} else {
$data['lastname'] = $this->model_user->getUserByLastname($user_id);
}
if (!empty($this->input->post('email'))) {
$data['email'] = $this->input->post('email');
} else {
$data['email'] = $this->model_user->getUserByEmail($user_id);
}
if(!empty($this->input->post('image'))) {
$data['image'] = $this->input->post('image');
} else {
$data['image'] = $this->model_user->getUserImage($user_id);
}
$data['image_path'] = config_item('HTTP_CATALOG') . '/image/catalog/';
$this->load->library('password');
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Your Username');
$this->form_validation->set_rules('firstname', 'Your First Name');
$this->form_validation->set_rules('lastname', 'Your Last Name');
$this->form_validation->set_rules('email', 'Your Email');
if ($this->form_validation->run() == TRUE) {
$this->model_user->editUser($user_id, $data);
redirect('users');
} else {
$data['header'] = $this->load->view('template/common/header', $data, TRUE);
$data['footer'] = $this->load->view('template/common/footer', NULL, TRUE);
return $this->load->view('template/users/users_form', $data);
}
}
View
<?php echo validation_errors('<div class="alert alert-danger">', '</div>'); ?>
<?php
$data = array(
'role' => "form",
'class' => 'form-horizontal'
);
echo form_open('users/edit/' . $user_id, $data);
?>
<div class="form-group">
<label for="input-username" class="col-sm-2 control-label">Username</label>
<div class="col-sm-10">
<input type="text" name="username" value="<?php echo $username;?>" class="form-control">
</div>
</div>
<div class="form-group">
<label for="input-firstname" class="col-sm-2 control-label">Firstname</label>
<div class="col-sm-10">
<input type="text" name="firstname" value="<?php echo $firstname;?>" class="form-control">
</div>
</div>
<div class="form-group">
<label for="input-lastname" class="col-sm-2 control-label">Lastname</label>
<div class="col-sm-10">
<input type="text" name="lastname" value="<?php echo $lastname;?>" class="form-control">
</div>
</div>
<div class="form-group">
<label for="input-email" class="col-sm-2 control-label">Email</label>
<div class="col-sm-10">
<input type="text" name="email" value="<?php echo $email;?>" class="form-control">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label" for="input-image"></label>
<div class="col-lg-3">
<div class="thumbnail">
<img src="<?php echo $image_path . $image; ?>" style=" width: 100%; height: 300px;">
</div>
</div>
</div>
<div class="form-group">
<label for="input-image" class="col-sm-2 control-label">Image</label>
<div class="col-sm-10">
<br>
<input type="file" name="image" value="<?php echo $image;?>" size="20">
</div>
</div>
<div class="form-group">
<div class="text-right">
<h2><a href="<?php echo base_url('users/edit_password/' . $user_id);?>">Change Your Password</h2>
<button type="submit" name="submit" class="btn btn-primary">Submit</button>
</div>
</div>
<?php echo form_close();?>

Categories