upload profile photo not working in codeigniter 3 - php

i want to allow my user to update their profile and profile photo. if i comment the profile photo part in view and get rid in controller. the edit went well, but if i put the upload part in controller, the edit function not working. btw i'm still new in codeigniter. can you help me find my mistake.. thank you
my controller
public function edit($id){
$this->load->model('User_model');
$user = $this->User_model->getUser($id);
$data = array();
$data['user'] = $user;
$data1['user'] = $this->db->get_where('user', ['email' =>
$this->session->userdata('email')])->row_array();
$this->form_validation->set_rules('name', 'Name', 'required|trim');
$this->form_validation->set_rules('email', 'Email', 'required|trim|valid_email');
$this->form_validation->set_rules('password1', 'Password', 'required|trim|min_length[8]|matches[password2]', [
'matches' => 'password not match!',
'min_length' => 'password too short'
]);
$this->form_validation->set_rules('password2', 'Password', 'required|trim|matches[password1]');
if ($this->form_validation->run() == false) {
$this->load->view('templates/admin/header', $data, $data1);
$this->load->view('templates/admin/sidebar', $data, $data1);
$this->load->view('templates/admin/topbar', $data1);
$this->load->view('admin/updateprofile', $data, $data1);
$this->load->view('templates/admin/footer', $data, $data1);
} else {
$upload_image = $_FILES['image']['name'];
if ($upload_image) {
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '2048';
$config['upload_path'] = './assets/img/proile/';
$this->load->library('upload', $config);
if ($this->upload->do_upload('image')) {
$old_image = $data['user']['image'];
if ($old_image != 'default.jpg') {
unlink(FCPATH . 'assets/img/profile/' . $old_image);
}
$new_image = $this->upload->data('file_name');
$this->db->set('image', $new_image);
} else {
echo $this->upload->display_errors();
}
}
$data = array(
'name' => htmlspecialchars($this->input->post('name', true)),
'password' => password_hash($this->input->post('password1'), PASSWORD_DEFAULT),
);
$this->User_model->updateUser($id, $data);
$this->session->set_flashdata('message', '<div class="alert alert-success" role="alert">
User update successfully!
</div>');
redirect('adminprofile');
}
}
}
my model
function getUser($id)
{
$this->db->where('id', $id);
return $user = $this->db->get('user')->row_array();
}
function updateUser($id, $data)
{
$this->db->where('id', $id);
$this->db->update('user', $data);
}
my view
<div class="container-fluid">
<h1 class="h3 mb-4 text-gray-800">Edit Profile</h1>
<div class="row">
<div class="col-lg-8">
<form class="user" method="post" action="<?php echo base_url() . 'AdminUpdateProfile/edit/' . $user['id']; ?>">
<div class="form-group row">
<label for="name" class="col-sm-2 col-form-label">Nama</label>
<div class="col-sm-10">
<input type="name" class="form-control" id="name" name="name" value="<?= set_value('name', $user['name']); ?>">
</div>
</div>
<div class="form-group row">
<label for="email" class="col-sm-2 col-form-label">Emel</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="email" name="email" value="<?= set_value('email', $user['email']); ?>" readonly>
</div>
</div>
<div class="form-group row">
<label for="email" class="col-sm-2 col-form-label">Kata Laluan</label>
<div class="col-sm-10">
<input type="password" class="form-control" id="password1" name="password1" placeholder="8 aksara">
<?= form_error('email', '<small class="text-danger pl-3">', '</small>'); ?>
</div>
</div>
<div class="form-group row">
<label for="email" class="col-sm-2 col-form-label">Ulang Kata Laluan</label>
<div class="col-sm-10">
<input type="password" class="form-control" id="password2" name="password2" placeholder="Masukkan semula">
<?= form_error('email', '<small class="text-danger pl-3">', '</small>'); ?>
</div>
</div>
<div class="form-group row">
<div class="col-sm-2">Gambar Profil</div>
<div class="col-sm-10">
<div class="row">
<div class="col-sm-3">
<img src="<?= base_url('assets/img/profile/') . $user['image']; ?>" class="img-thumbnail">
</div>
<div class="col-sm-9">
<div class="custom-file">
<input type="file" class="custom-file-input" id="image" name="image">
<label class="custom-file-label" for="image">Choose file</label>
</div>
</div>
</div>
</div>
</div>
<div class="form-group row justify-content-end">
<div class="col-sm-10">
<button type="submit" class="btn btn-primary">Edit</button>
</div>
</div>
</form>
</div>
</div>
</div>
<!--<div class="container">
<div class="card o-hidden border-0 shadow-lg my-5 col-lg-7 mx-auto">
<div class="card-body p-0">
<div class="row">
<div class="col-lg">
<div class="p-5">
<div class="text-center">
<img src="<//?= base_url('assets/img/profile/') . $user['image']; ?>">
</div>
<hr>
<?= $this->session->set_flashdata('message'); ?>
<form class="user" method="post" action="<?php echo base_url() . 'AdminUpdateProfile/edit/' . $user['id']; ?>">
<div class="form-group row">
<input type="text" class="form-control" id="name" name="name" value="<?= set_value('name', $user['name']); ?>">
//<?= form_error('name', '<small class="text-danger pl-3">', '</small>'); ?>
</div>
<div class=" form-group row">
<input type="text" class="form-control" id="email" name="email" value="<?= set_value('email', $user['email']); ?>" readonly>
<?= form_error('email', '<small class="text-danger pl-3">', '</small>'); ?>
</div>
<div class="form-group row">
<input type="password" class="form-control" id="password1" name="password1" placeholder="Password">
<?= form_error('password1', '<small class="text-danger pl-3">', '</small>'); ?>
</div>
<div class="form-group row">
<input type="password" class="form-control" id="password2" name="password2" placeholder="Repeat Password">
</div>
<button type="submit" class="btn btn-primary btn-user btn-block">
Update
</button>
Cancel
</div>
</form>
</div>
</div>
</div>
</div> -->

First you have to use this after action attribute in form tag :
enctype='multipart/form-data'

You need to specify the enctype just like Aman said
<form class="user" method="post" action="<?= base_url('AdminUpdateProfile/edit/'.$user['id']); ?>" enctype="multipart/form-data">
your code here
</form>

As two of the above answers mention, first you need to add the encryption type to your form. So the end result would be:
<form method="post" enctype="multipart/form-data" class="user" action="<?php echo base_url() . 'AdminUpdateProfile/edit/' . $user['id']; ?>">
If this still doesn't work, in my case I had to convert the $_FILES indexes to $_FILES['userfile'] because of an issue with the upload library where it had issues with indexes that weren't userfile. I basically did this:
$this->load->library('upload');
$config['upload_path'] = 'images/';
$config['file_ext_tolower'] = TRUE;
$config['allowed_types'] = '*';
$this->upload->initialize($config);
$_FILES['userfile']['name'] = $_FILES['image']['name'];
$_FILES['userfile']['type'] = $_FILES['image']['type'];
$_FILES['userfile']['tmp_name'] = $_FILES['image']['tmp_name'];
$_FILES['userfile']['error'] = $_FILES['image']['error'];
$_FILES['userfile']['size'] = $_FILES['image']['size'];
$this->upload->do_upload();
$data = $this->upload->data();

Related

codeigniter form_validation not working and $this->form_validation->run() don't return anything

This is my view page code
i check codeigniter form validation in codeigniter it's copy and paste my controller but same error
<form action="<?php echo base_url(); ?>admin/user_setup/store" method="post" class="ml-4 mr-4 mt-2" enctype="multipart/form-data">
<div class="row">
<div class="col-lg-3 col-md-6 col-sm-12">
<div class="form-group form__group">
<input type="text" name="firstname" id="firstname" placeholder="নাম" class="form-control form__field" required>
<label for="firstname" class="form__label">নাম </label>
</div>
</div>
<div class="col-lg-3 col-md-6 col-sm-12">
<div class="form-group form__group">
<input type="text" name="username" id="username" placeholder="ইউজার নাম" class="form-control form__field" required>
<label for="username" class="form__label">ইউজার নাম </label>
</div>
</div>
<div class="col-lg-3 col-md-6 col-sm-12">
<div class="form-group form__group">
<input type="text" name="email" id="email" placeholder="ই-মেইল" class="form-control form__field" required>
<label for="email" class="form__label">ই-মেইল </label>
</div>
</div>
<div class="col-lg-3 col-md-6 col-sm-12">
<div class="form-group form__group">
<select name="activated" id="activated" class="form-control form__field" >
<option value="1">Active</option>
<option value="0">Inactive</option>
</select>
<label for="activated" class="form__label">অ্যাক্টিভেটেড</label>
</div>
</div>
<div class="col-lg-3 col-md-6 col-sm-12">
<div class="form-group form__group">
<input type="password" name="password" id="password" placeholder="পাসওয়ার্ড" class="form-control form__field" required>
<label for="password" class="form__label">পাসওয়ার্ড</label>
</div>
</div>
<div class="col-lg-3 col-md-6 col-sm-12">
<div class="form-group form__group">
<input type="password" name="confirm_password" id="confirm_password" placeholder="কন্ফার্ম পাসওয়ার্ড" class="form-control form__field" required>
<label for="confirm_password" class="form__label">কন্ফার্ম পাসওয়ার্ড</label>
</div>
</div>
<div class="col-lg-2 col-md-6 col-sm-12">
<div class="form-group form__group">
<label for="logo" class="form__label">ছবি </label>
<input type="file" name="logo" id="logo" placeholder="ছবি" class="form-control form__field" rows="2" cols="2" style="height: 38px;"></input>
</div>
</div>
<div class="col-lg-2 col-md-6 col-sm-12">
<div class="form-group form__group">
<select name="type" id="type" class="form-control form__field" >
<option value="2">এডিটর</option>
<option value="3">লাইসেন্স অফিসার</option>
</select>
<label for="type" class="form__label">ইউজার টাইপ</label>
</div>
</div>
<div class="col-lg-2 col-md-6 col-sm-12">
<div class="form-group">
<input type="submit" value="তৈরি করুন" class="form-control btn btn-sm btn-primary mt-fl">
</div>
</div>
</div>
</form>
this is my controller code
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean|min_length[6]|max_length[100]|alpha_dash');
$this->form_validation->set_rules('firstname', 'First Name', 'trim|required|xss_clean|min_length[6]|max_length[100]|alpha_dash');
$this->form_validation->set_rules('email', 'Email', 'trim|required|xss_clean|valid_email');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|minlength[6]|max_length[50]|alpha_dash');
$this->form_validation->set_rules('confirm_password', 'Confirm Password', 'trim|required|xss_clean|matches[password]');
$data['errors'] = array();
echo $this->form_validation->set_value('username');
echo $this->input->post('username');
if ($this->form_validation->run()) {
$data['username'] = $this->form_validation->set_value('username');
$data['firstname'] = $this->form_validation->set_value('firstname');
$data['email'] = $this->form_validation->set_value('email');
$data['password'] = $this->form_validation->set_value('password');
$data['created'] = date('Y-m-d H:i:s');
$data['activated'] = $this->input->post('activated');
$data['type'] = $this->input->post('type');
$insert_id = $this->web_model->insert('users', $data);
var_dump("asduasiduiasdijasidij");
} else {
$errors = $this->tank_auth->get_error_message();
foreach ($errors as $k => $v) $data['errors'][$k] = $this->lang->line($v);
// $this->render_page('admin/setup/user_setup/create', $data);
echo $this->form_validation->run();
var_dump($data['errors']);
}
form validation don't return anything and $this->form_validation->set_value('any'); is always be empty
I load form_validation in controller __construct and also check load function inner but error is absolute
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
Check your PHPMyAdmin, I got the same problem last month, find xampp with build in PHP 5.6, link here https://sourceforge.net/projects/xampp/files/XAMPP%20Windows/5.6.40/ , delete or rename your old Windows(c:)/xammp/ and replace with PHP 5.6

CodeIgniter - Update Database Record

I've done the coding of Update Database record in Codeigniter. Everything works fine but When I click the Save button database records won't update. Each time it's showing the old database records. I want to update the database records but what's wrong with this. I don't understand the missing thing. Help Needed.
My Controller:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Employee extends CI_Controller {
function __construct()
{
parent::__construct();
if($this->session->email == "")
{
redirect('login');
}
$this->load->model('EmployeeModel','EmployeeModel');
}
public function ViewEmployee()
{
$data['getEmployee'] = $this->EmployeeModel->getEmployee();
$this->load->view('view_employee',$data);
}
public function update($empID) {
$data['user'] = $this->EmployeeModel->get_by($empID);
$data['subview'] = 'Employee/edit_employee';
$this->load->view('edit_employee', $data);
}
public function edit() {
$empID = $this->input->post('empID');
//Check whether user upload picture
if(!empty($_FILES['picture']['name'])){
$config['upload_path'] = 'uploads/';
$config['allowed_types'] = 'jpg|jpeg|png|gif';
$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 = '';
}
}else{
$picture = '';
}
$data = array(
'empFirstName' => $this->input->post('firstname'),
'empLastName' => $this->input->post('lastname'),
'empNO' => $this->input->post('employee_id'),
'designation' => $this->input->post('designation'),
);
$this->db->where('empID', $empID);
$this->db->update('tbl_employee', $data);
redirect('view_employee');
$this->update($empID);
}
}
My Model:
<?php
class EmployeeModel extends CI_Model
{
function __construct() {
parent::__construct();
}
public function EmployeeRegister($data)
{
$this->db->insert('tbl_employee',$data);
return true;
}
public function getEmployee()
{
$this->db->where('status',0);
$query = $this->db->get('tbl_employee');
return $query->result();
}
public function get_by($empID) {
$this->db->where('empID', $empID);
$user = $this->db->get('tbl_employee')->row();
return $user;
}
}
View:
<?php
print_r($user)
?>
<?php echo form_open('Employee/edit'); ?>
<div class="row">
<div class="form-titles"> Personal Information</div>
<div class="col-lg-4 input_field_sections">
<h5>Employee Name <span style="color: #cc0000">*</span></h5>
<input required type="text" class="form-control" name="firstname" value="<?=$user->empFirstName?>"/>
</div>
<div class="col-lg-4 input_field_sections">
<h5>Father's Name <span style="color: #cc0000">*</span></h5>
<input required type="text" class="form-control" name="lastname" value="<?=$user->empLastName?>"/>
</div>
</div>
<div class="row">
<div class="col-lg-4 input_field_sections">
<h5>NIC <span style="color: #cc0000">*</span></h5>
<input required type="text" class="form-control" name="cnic" value="<?=$user->cnic?>"/>
</div>
<div class="col-lg-4 input_field_sections">
<h5>Date of Birth <span style="color: #cc0000">*</span></h5>
<input required="" type="text" class="form-control datepicker" name="birth_date" value="<?=$user->birth_date?>"/>
</div>
</div>
<div class="row">
<div class="col-lg-4 input_field_sections">
<h5>Mobile #<span style="color: #cc0000">*</span></h5>
<input required="" type="text" class="form-control" name="mobile" value="<?=$user->empMobile?>"/>
</div>
<div class="col-lg-4 input_field_sections">
<h5>Phone #</h5>
<input type="text" class="form-control" name="phone" value="<?=$user->phone?>"/>
</div>
</div>
<div class="row">
<div class="col-lg-4 input_field_sections">
<h5>Email ID</h5>
<input type="email" class="form-control" name="email" value="<?=$user->empemail?>"/>
</div>
<div class="col-lg-4 input_field_sections">
<h5>Current Photo</h5>
<input type="file" class="form-control" name="picture" value="<?=$user->picture?>"/>
<img src="<?php echo base_url().'uploads/'.$user->picture?>" style="width: 150px;height: auto;" />
</div>
</div>
<div class="row">
<div class="col-lg-8 input_field_sections">
<h5>Address / City <span style="color: #cc0000">*</span></h5>
<textarea required="" name="address" class="addres form-control" /><?=$user->empaddress?></textarea>
</div>
</div>
<div class="row">
<div class="form-titles" style="margin-top:40px;"> Job Information</div>
<div class="col-lg-4 input_field_sections">
<h5>Work Location <span style="color: #cc0000">*</span></h5>
<input required type="text" class="form-control" name="worklocation" value="<?=$user->worklocation?>"/>
</div>
<div class="col-lg-4 input_field_sections">
<h5>Employee ID <span style="color: #cc0000">*</span></h5>
<input required type="text" class="form-control" name="employee_id" value="<?=$user->empNO?>"/>
</div>
</div>
<div class="row">
<div class="col-lg-4 input_field_sections">
<h5>Department <span style="color: #cc0000">*</span></h5>
<input required="" type="text" class="form-control" name="department" value="<?=$user->department?>"/>
</div>
<div class="col-lg-4 input_field_sections">
<h5>Designation <span style="color: #cc0000">*</span></h5>
<input required type="text" class="form-control" name="designation" value="<?=$user->designation?>"/>
</div>
</div>
<div class="col-lg-8 input_field_sections">
<button type="submit" class="btn btn-primary">Save</button>
</div>
</form>
Use this one to debug your application.
echo $this->db->last_query();

missing argument edit data with upload image in codeigniter

i think i have correctly writting this code but i get a problem on missing argument and i selected file image to upload but i get error too like this.
A PHP Error was encountered
Severity: Warning
Message: Missing argument 1 for Data_guru::edit()
Filename: tim_monitoring/Data_guru.php
Line Number: 90
Backtrace:
File: C:\xampp\htdocs\sdb\application\controllers\tim_monitoring\Data_guru.php
Line: 90
Function: _error_handler
File: C:\xampp\htdocs\sdb\index.php
Line: 315
Function: require_once
You did not select a file to upload.
thanks for your answer
My Controller
public function edit($id) {
$this->form_validation->set_rules('nama_guru','Nama Guru','required');
$this->form_validation->set_rules('alamat','Alamat','required');
if ($this->form_validation->run() === FALSE) {
$data = array ('title' => 'Edit Data Guru',
'detail' => $this->monitoring_model->detail_guru($id),
'isi' => 'monitoring/edit_guru_view'
);
$this->load->view('monitoring/layout/wrapper',$data);
//Kalau Tidak Ada Error Data Guru DiUpdate
}else{
$config['file_name'] = $this->input->post('nama_guru');
$config['upload_path'] = './assets/image/guru/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 3000;
$config['max_width'] = 3000;
$config['max_height'] = 3000;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('foto_guru'))
{
echo $this->upload->display_errors();
}
else
{
$gbr = $this->upload->data();
$data = array(
'foto_guru' => $gbr['file_name'],
'id_guru' => $this->input->post('id_guru'),
'nama_guru' => $this->input->post('nama_guru'),
'jenis_kelamin' => $this->input->post('jen_kel'),
'alamat' => $this->input->post('alamat'),
'tempat_lahir' => $this->input->post('tempat_lahir'),
'tgl_lahir' => $this->input->post('tgl_lahir'),
'no_hp' => $this->input->post('no_hp'),
'username' => $this->input->post('username'),
'password' => $this->input->post('password')
);
$this->monitoring_model->edit_guru($data);
redirect(base_url().'tim_monitoring/data_guru');
}
}
}
My Model
//Menampilkan Detail Guru Di Halaman Edit Guru
public function detail_guru($id) {
$query = $this->db->get_where('t_guru', array('id_guru' => $id));
return $query->row_array();
}
//Update Data Guru Setelah Di Edit Di Halaman Edt
public function edit_guru($data) {
$this->db->where('id_guru',$data['id_guru']);
return $this->db->update('t_guru',$data);
}
My View
<form action="<?php echo base_url() ?>/tim_monitoring/data_guru/edit" class="form-horizontal" method="post">
<div class="form-group">
<label for="inputEmail3" class="col-sm-4 control-label">Nama Guru</label>
<div class="col-sm-6">
<input type="text" name="nama_guru" class="form-control" placeholder="Nama Guru" value="<?php echo $detail['nama_guru'] ?>" required>
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-4 control-label">Tempat Lahir</label>
<div class="col-sm-6">
<input type="text" name="tempat_lahir" class="form-control" placeholder="Tempat Lahir" value="<?php echo $detail['tempat_lahir'] ?>" required>
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-4 control-label">Tanggal Lahir</label>
<div class="col-sm-6">
<input type="date" name="tgl_lahir" class="form-control" value="<?php echo $detail['tgl_lahir'] ?>" required>
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-4 control-label">Alamat</label>
<div class="col-sm-6">
<textarea name="alamat" class="form-control" rows="4" required><?php echo $detail['alamat'] ?></textarea>
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-4 control-label">No Handphone</label>
<div class="col-sm-6">
<input type="number" name="no_hp" class="form-control" placeholder="No Handphone" value="<?php echo $detail['no_hp'] ?>" required>
</div>
</div>
<div class="form-group">
<label class="col-sm-4 control-label">Foto</label>
<div class="col-sm-6">
<input type="file" name="foto_guru" id="exampleInputFile" required>
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-4 control-label">Username</label>
<div class="col-sm-6">
<input type="text" name="username" class="form-control" placeholder="Username" value="<?php echo $detail['username'] ?>" required>
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-4 control-label">Password</label>
<div class="col-sm-6">
<input type="password" name="password" class="form-control" placeholder="Password" value="<?php echo $detail['password'] ?>" required>
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-4 control-label">Jenis Kelamin</label>
<div class="col-sm-6">
<input type="radio" name="jen_kel" value="L"><label> Laki - Laki</label>
<input type="radio" name="jen_kel" value="P"><label> Perempuan</label>
</div>
</div>
<input type="hidden" name="id_guru" class="form-control" value="<?php echo $detail['id_guru'] ?>" required>
<div class="form-group">
<div class="col-sm-4">
</div>
<div class="col-sm-6">
<button type="submit" class="btn btn-primary">Ubah Data</button>
</div>
</div>
</form>
Please see edit() Method of tim_monitoring/Data_guru.php that's must be require to pass one parameter as value of variable $id.
So, you need to add following URL in form's action
action="<?php echo base_url() ?>/tim_monitoring/data_guru/edit/<?php echo $detail['id_guru'] ?>"
For upload image form must require attribute enctype="multipart/form-data"

Uploading Image File in Codeigniter 3 returns error 500

So I'm trying to insert in the database and upload a file. But when I'm trying to submit, it returns a server error.
The same code is working on my other project but it is using CI2, i checked the .htaccess and its all the same
The localhost page isn’t working
localhost is currently unable to handle this request.
HTTP ERROR 500
I think the problem is in the $this->upload->do_upload('userfile') because i tried to comment out from this part and display echo the config array and there's no error but when i tried to uncomment up to this part, the error shows.
Controller
public function add_now(){
$this->load->library('form_validation');
$this->form_validation->set_rules('Event_Name', 'Event Name', 'trim|strip_tags');
$this->form_validation->set_rules('Event_Start', 'Start Date', 'trim|strip_tags');
$this->form_validation->set_rules('Event_End', 'End Date', 'trim|strip_tags');
$this->form_validation->set_rules('Event_Location', 'Location', 'trim|strip_tags');
if($this->form_validation->run() == FALSE){
$this->add();
}
else{
$query = $this->events_model->do_upload();
if($query){
$this->session->set_flashdata('success', 'Successful!');
$this->index();
}
else{
if(!$this->session->flashdata('upload_error')){
$this->session->set_flashdata('failed', 'Failed!');
}
$this->add();
}
}
}
Model
public function do_upload(){
$config['upload_path'] = './resources/images/events_photo/temp/';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['file_name'] = uniqid().'.jpeg';
$config['overwrite'] = TRUE;
$this->load->library('upload', $config);
if(!$this->upload->do_upload('userfile')){
$this->session->set_flashdata('upload_error', $this->upload->display_errors());
}
}
View
<?php echo form_open_multipart('events/add_now'); ?>
<div class="panel-body">
<div class="row">
<div class="form-group">
<label class="control-label">Name</label>
<input type="text" class="form-control" name="Event_Name" value="<?php echo set_value('Event_Name'); ?>" placeholder="Enter Event Name">
<small class="text-danger"><?php echo form_error('Event_Name'); ?></small>
</div>
</div>
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<label class="control-label">Start Date</label>
<input type="text" class="form-control" name="Event_Start" value="<?php echo set_value('Event_Start'); ?>" placeholder="Enter Start Date" onclick="this.type='datetime-local'" onblur="this.type='text'" min="<?php echo date('Y-m-d'); ?>">
<small class="text-danger"><?php echo form_error('Event_Start'); ?></small>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label class="control-label">End Date</label>
<input type="text" class="form-control" name="Event_End" value="<?php echo set_value('Event_End'); ?>" placeholder="Enter End Date" onclick="this.type='datetime-local'" onblur="this.type='text'" min="<?php echo date('Y-m-d'); ?>">
<small class="text-danger"><?php echo form_error('Event_End'); ?></small>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-9">
<div class="form-group">
<label class="control-label">Location</label>
<input type="text" class="form-control" name="Event_Location" value="<?php echo set_value('Event_Location'); ?>" placeholder="Enter Event Location">
<small class="text-danger"><?php echo form_error('Event_Location'); ?></small>
</div>
</div>
</div>
<div class="row">
<textarea placeholder="Event Description" name="Event_Description" rows="10" class="form-control"></textarea>
</div>
<div class="row">
<div class="form-group">
<label class="control-label">Image Attachment (Optional)</label>
<input type="file" class="form-control" name="userfile" value="<?php echo set_value('userfile'); ?>" placeholder="Upload Image">
<?php if($this->session->flashdata('upload_error')): ?>
<small class="text-danger"><?php echo $this->session->flashdata('upload_error'); ?></small>
<?php endif; ?>
</div>
</div>
</div>
<div class="panel-footer text-right">
<button class="btn btn-success" type="submit">Submit</button>
</div>
</form>

Codeigniter File Upload and Database Insert In 1 form

So I'm trying to insert in the database and upload a file. But when I'm trying to submit, it returns a server error.
The localhost page isn’t working
localhost is currently unable to handle this request.
HTTP ERROR 500
I think the problem is in the $this->upload->do_upload('userfile') because i tried to comment out from this part and display echo the config array and there's no error but when i tried to uncomment up to this part, the error shows.
Controller
public function add_now(){
$this->load->library('form_validation');
$this->form_validation->set_rules('Event_Name', 'Event Name', 'trim|strip_tags');
$this->form_validation->set_rules('Event_Start', 'Start Date', 'trim|strip_tags');
$this->form_validation->set_rules('Event_End', 'End Date', 'trim|strip_tags');
$this->form_validation->set_rules('Event_Location', 'Location', 'trim|strip_tags');
if($this->form_validation->run() == FALSE){
$this->add();
}
else{
$query = $this->events_model->do_upload();
if($query){
$this->session->set_flashdata('success', 'Successful!');
$this->index();
}
else{
if(!$this->session->flashdata('upload_error')){
$this->session->set_flashdata('failed', 'Failed!');
}
$this->add();
}
}
}
Model
public function do_upload(){
$config['upload_path'] = './resources/images/events_photo/temp/';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['file_name'] = uniqid().'.jpeg';
$config['overwrite'] = TRUE;
$this->load->library('upload', $config);
if(!$this->upload->do_upload('userfile')){
$this->session->set_flashdata('upload_error', $this->upload->display_errors());
}
}
View
<?php echo form_open_multipart('events/add_now'); ?>
<div class="panel-body">
<div class="row">
<div class="form-group">
<label class="control-label">Name</label>
<input type="text" class="form-control" name="Event_Name" value="<?php echo set_value('Event_Name'); ?>" placeholder="Enter Event Name">
<small class="text-danger"><?php echo form_error('Event_Name'); ?></small>
</div>
</div>
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<label class="control-label">Start Date</label>
<input type="text" class="form-control" name="Event_Start" value="<?php echo set_value('Event_Start'); ?>" placeholder="Enter Start Date" onclick="this.type='datetime-local'" onblur="this.type='text'" min="<?php echo date('Y-m-d'); ?>">
<small class="text-danger"><?php echo form_error('Event_Start'); ?></small>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label class="control-label">End Date</label>
<input type="text" class="form-control" name="Event_End" value="<?php echo set_value('Event_End'); ?>" placeholder="Enter End Date" onclick="this.type='datetime-local'" onblur="this.type='text'" min="<?php echo date('Y-m-d'); ?>">
<small class="text-danger"><?php echo form_error('Event_End'); ?></small>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-9">
<div class="form-group">
<label class="control-label">Location</label>
<input type="text" class="form-control" name="Event_Location" value="<?php echo set_value('Event_Location'); ?>" placeholder="Enter Event Location">
<small class="text-danger"><?php echo form_error('Event_Location'); ?></small>
</div>
</div>
</div>
<div class="row">
<textarea placeholder="Event Description" name="Event_Description" rows="10" class="form-control"></textarea>
</div>
<div class="row">
<div class="form-group">
<label class="control-label">Image Attachment (Optional)</label>
<input type="file" class="form-control" name="userfile" value="<?php echo set_value('userfile'); ?>" placeholder="Upload Image">
<?php if($this->session->flashdata('upload_error')): ?>
<small class="text-danger"><?php echo $this->session->flashdata('upload_error'); ?></small>
<?php endif; ?>
</div>
</div>
</div>
<div class="panel-footer text-right">
<button class="btn btn-success" type="submit">Submit</button>
</div>
</form>
First load the url helper in your controller...
$this->load->helper('url');
Then set the upload file path as:
$config['upload_path'] = base_url('resources/images/events_photo/temp/');
Try it might works.
And what about these two functions
$this->add();
$this->success();

Categories