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.
Related
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();
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 {
}
This is my code, my photo can't be inserted into the database.
Actually I want to make an online exam with the codeigniter. I want to upload the question with the pic. but when I tried to upload the pict, the code is not working.
but the question success inserted into the database. only the pict failed to upload
Controller:
function insert(){
$nama_asli = $_FILES['userfile']['name'];
$config ['file_name'] = $nama_asli;
$config ['upload_path'] = './images';
$config ['allowed_types'] = 'gif|jpg|png|jpeg';
$config ['max_size'] = '2500';
$this->load->library('upload', $config);
$upload_data = $this->upload->data();
$file_name = $upload_data['file_name'];
$id_soal = '';
$soal = $_POST['soal'];
$a = $_POST['a'];
$b = $_POST['b'];
$c = $_POST['c'];
$d = $_POST['d'];
$kunci = $_POST['kunci'];
$status = $_POST['status'];
$data = array(
'id_soal' => $id_soal,
'soal' => $soal,
'a' => $a,
'b' => $b,
'c' => $c,
'd' => $d,
'kunci' => $kunci,
'status' => $status,
'foto' => $file_name,
);
$hasil = $this->soal_model->Simpan('soal', $data);
if($hasil>=1){
redirect('dashboard/index', $data);
}
}
Model:
class Soal_model extends Ci_Model {
public function Ambil($where= "") {
$data = $this->db->query('select * from soal '.$where);
return $data;
}
public function Simpan($tabel, $data){
$res = $this->db->insert($tabel, $data);
return $res;
}
View:
<form role="form" action="<?php echo base_url(); ?>dashboard/insert" method="POST" enctype="multipart/form-data">
<form class="form-horizontal" method="post" style = "margin : 10px;">
<div class = "row">
<div class = "col-sm-offset-3 col-sm-6">
<div class="form-group">
<label>Soal :</label>
<textarea type="text" class="form-control" name="soal" id="soal" required></textarea>
</div>
<div class="form-group">
<label>Jawaban A :</label>
<input type="text" class="form-control"
placeholder="Type Here" name="a" id="a" required/>
</div>
<div class="form-group">
<label>Jawaban B :</label>
<input type="text" class="form-control"
placeholder="Type Here" name="b" id="b" required/>
</div>
<div class="form-group">
<label>Jawaban C :</label>
<input type="text" class="form-control"
placeholder="Type Here" name="c" id="c" required/>
</div>
<div class="form-group">
<label>Jawaban D :</label>
<input type="text" class="form-control"
placeholder="Type Here" name="d" id="d" required/>
</div>
<div class="form-group">
<label>Kunci :</label>
<select name="kunci" id="kunci" class="form-control">
<option>Select</option>
<option>A</option>
<option>B</option>
<option>C</option>
<option>D</option>
</select>
</div>
<div class="form-group">
<label>Status :</label>
<select name="status" id="status" class="form-control">
<option value="">Select</option>
<option value="tampil">Tampil</option>
<option value="tidak">Tidak</option>
</select>
<div class="form-group">
<label>Photo :</label>
<input type="file" name="foto" id="foto" size="20"/>
</div>
<br>
<div class="form-group">
<button type="submit" class="btn btn-default">Simpan</button>
<button type="reset" class="btn btn-default">Hapus</button>
</div>
</div>
</div>
</form>
Database:
Kindly use this code
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Dashboard extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->model('soal_model');
}
function insert()
{
$config =array(
'upload_path' => './images',
'allowed_types' => 'gif|jpg|png|jpeg',
'max_size' => '2500',
);
$this->load->library('upload', $config);
$this->upload->do_upload('file_upload');
$upload_data = $this->upload->data();
$file_name = $upload_data['file_name'];
$data = array(
'foto' => $file_name,
);
$hasil = $this->soal_model->Simpan('soal', $data);
if($hasil>=1){
redirect('dashboard/index', $data);
}
}
?>
This question already has an answer here:
Upload Photo Failed Codeigniter
(1 answer)
Closed 6 years ago.
This is my code, my photo can't be inserted into the database. I don't know where the problem is.
actually i want to make an online exam with the codeigniter. i want to upload the question with the pic. but when i tried to upload the pict, the code is not working.
but the question success inserted into the database. only the pict failed to upload
Controller :
function insert(){
$nama_asli = $_FILES['userfile']['name'];
$config ['file_name'] = $nama_asli;
$config ['upload_path'] = './images/';
$config ['allowed_types'] = 'gif|jpg|png|jpeg';
$config ['max_size'] = '2500';
$config ['max_width'] = '2600';
$config ['max_height'] = '2200';
$id_soal = '';
$soal = $_POST['soal'];
$a = $_POST['a'];
$b = $_POST['b'];
$c = $_POST['c'];
$d = $_POST['d'];
$kunci = $_POST['kunci'];
$status = $_POST['status'];
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload()){
$error = array('error' => $this->upload->display_errors());
$this->load->view('admin/home/create_admin', $error);
}
else{
$data = array('upload_data' => $this->upload->data());
$upload_data = $this->upload->data();
$file_name = $upload_data['file_name'];
$data = array(
'id_soal' => $id_soal,
'soal' => $soal,
'a' => $a,
'b' => $b,
'c' => $c,
'd' => $d,
'kunci' => $kunci,
'status' => $status,
'foto' => $file_name,
);
$hasil = $this->soal_model->Simpan('soal', $data);
if($hasil>=1){
redirect('dashboard/index', $data);
}
}
}
Model :
class Soal_model extends Ci_Model {
public function Ambil($where= "") {
$data = $this->db->query('select * from soal '.$where);
return $data;
}
public function Simpan($tabel, $data){
$res = $this->db->insert($tabel, $data);
return $res;
}
View:
<form role="form" action="<?php echo base_url(); ?>dashboard/insert" method="POST" enctype="multipart/form-data">
<form class="form-horizontal" method="post" style = "margin : 10px;">
<div class = "row">
<div class = "col-sm-offset-3 col-sm-6">
<div class="form-group">
<label>Soal :</label>
<textarea type="text" class="form-control" name="soal" id="soal" required></textarea>
</div>
<div class="form-group">
<label>Jawaban A :</label>
<input type="text" class="form-control"
placeholder="Type Here" name="a" id="a" required/>
</div>
<div class="form-group">
<label>Jawaban B :</label>
<input type="text" class="form-control"
placeholder="Type Here" name="b" id="b" required/>
</div>
<div class="form-group">
<label>Jawaban C :</label>
<input type="text" class="form-control"
placeholder="Type Here" name="c" id="c" required/>
</div>
<div class="form-group">
<label>Jawaban D :</label>
<input type="text" class="form-control"
placeholder="Type Here" name="d" id="d" required/>
</div>
<div class="form-group">
<label>Kunci :</label>
<select name="kunci" id="kunci" class="form-control">
<option>Select</option>
<option>A</option>
<option>B</option>
<option>C</option>
<option>D</option>
</select>
</div>
<div class="form-group">
<label>Status :</label>
<select name="status" id="status" class="form-control">
<option value="">Select</option>
<option value="tampil">Tampil</option>
<option value="tidak">Tidak</option>
</select>
<div class="form-group">
<label>Photo :</label>
<input type="file" name="foto" id="foto" size="20"/>
</div>
<br>
<div class="form-group">
<button type="submit" class="btn btn-default">Simpan</button>
<button type="reset" class="btn btn-default">Hapus</button>
</div>
</div>
</div>
</form>
Database :
Anyone can help?
you need to add the do_upload method of upload::library
$nama_asli = $_FILES['userfile']['name'];
$config ['file_name'] = $nama_asli;
$config ['upload_path'] = './images/';
$config ['allowed_types'] = 'gif|jpg|png|jpeg';
$config ['max_size'] = '2500';
$this->load->library('upload', $config);
//you need to add this do_upload();
if ( ! $this->upload->do_upload('userfile')){
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else{
$data = array('upload_data' => $this->upload->data());
$upload_data = $this->upload->data();
$file_name = $this->upload->file_name;
.
.
.
...
}
I am trying to update my database through a form which includes updating the stored image in the database but it doesnt work nothing happens if i click the upload button it just redirects me
my view :
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<title>Test</title>
</head>
<body>
<div class="container">
<div class="row" >
<?php echo form_open_multipart('edit_news/update_news'); ?>
<div class="col-md-10">
<br>
<div class="panel panel-default">
<div class="panel-body">
<?php if (validation_errors()): ?>
<div class="alert alert-danger alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<?php echo validation_errors(); ?>
</div>
<?php endif ?>
<?php foreach ($news as $n): ?>
<form action="<?php echo base_url() . "edit_news/update_news/". $n->news_id; ?>" method="post" class="form-horizontal" role="form">
<div class="form-group">
<label class="col-sm-2 control-label" style=" color: white"></label>
<label class="col-sm-2 control-label">Product Image</label>
<div class="col-sm-5">
<input type="file" class="form-control" placeholder="" name="userfile">
</div>
</div>
<br>
<br> <br>
<div class="form-group"> <label class="col-sm-2 control-label" ></label>
<label class="col-sm-2 control-label" >Product ID:</label>
<div class="input-group dis" style="width: 320px;">
<input value="<?php echo $n->news_id; ?>" disabled="" type="text" class="form-control " aria-describedby="sizing-addon2" id="id" name="id">
</div>
</div>
<div class="form-group"> <label class="col-sm-2 control-label" ></label>
<label class="col-sm-2 control-label" >Product Title:</label>
<div class="input-group dis" style="width: 320px;">
<input value="<?php echo $n->title; ?>" type="text" class="form-control " aria-describedby="sizing-addon2" id="name" name="title">
</div>
</div>
<div class="form-group"> <label class="col-sm-2 control-label" ></label>
<label class="col-sm-2 control-label" >Product Description:</label>
<div class="input-group dis" style="width: 320px;">
<input value="<?php echo $n->news_description; ?>" type="text" class="form-control " aria-describedby="sizing-addon2" id="price" name="description">
</div>
</div>
<?php echo form_error('serial'); ?>
<div class="col-sm-offset-0 col-sm-12"><label class="col-sm-4 control-label" ></label>
<button type="button, submit" class="btn btn-primary " style="border-radius: 0;">
Upload
</button>
</div>
<?php endforeach; ?>
<?php echo form_close() ?>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<script src="<?= base_url(); ?>js/bootstrap.min.js"></script>
<script>
$(document).ready(function() {
$('#myTable').dataTable();
});
</script>
</body>
</html>
controller:
public function update_news(){
$config['upload_path'] = './assets/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '200000';
$config['max_width'] = '200000';
$config['max_height'] = '200000';
$config['new_image'] = './assets/';
$config['overwrite'] = TRUE;
$this->load->library('upload', $config);
$this->form_validation->set_rules('title', 'News Title', 'required|xss_clean');
$this->form_validation->set_rules('description', 'News Description', 'required|xss_clean');
if (!$this->upload->do_upload() || !$this->form_validation->run()) {
$error = array('error' => $this->upload->display_errors());
redirect('admin_news_adds');
} else {
$data = $this->upload->data();
$this->thumb($data);
$id = $this->uri->segment(3);
$file = array(
'img_name' => $data['raw_name'],
'thumb_name' => $data['raw_name'] . '_thumb',
'ext' => $data['file_ext'],
'news_date' => date("l , F j, Y "),
'title' => $this->input->post('title'),
'status' => 1,
'news_description' => $this->input->post('description'),
);
$this->User->update_news($file,$id);
$data = array('upload_data' => $this->upload->data());
redirect('admin_news_add');
}
}
model:
public function update_news($file,$id) {
$this->db->where('news_id',$id);
$this->db->update('news_table', $file);
}
Maybe any error when you upload the image, you can debug your php source why error happened, like adding var_dump($error) in 1st condition (if (!$this->upload->do_upload() || !$this->form_validation->run()) ) and comment "redirect('admin_news_adds');" just for debugging condition.
public function update_news(){
$config['upload_path'] = './assets/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '200000';
$config['max_width'] = '200000';
$config['max_height'] = '200000';
$config['new_image'] = './assets/';
$config['overwrite'] = TRUE;
$this->load->library('upload', $config);
$this->form_validation->set_rules('title', 'News Title', 'required|xss_clean');
$this->form_validation->set_rules('description', 'News Description', 'required|xss_clean');
if (!$this->upload->do_upload() || !$this->form_validation->run()) {
$error = array('error' => $this->upload->display_errors());
var_dump($error);
//redirect('admin_news_adds');
} else {
$data = $this->upload->data();
$this->thumb($data);
$id = $this->uri->segment(3);
$file = array(
'img_name' => $data['raw_name'],
'thumb_name' => $data['raw_name'] . '_thumb',
'ext' => $data['file_ext'],
'news_date' => date("l , F j, Y "),
'title' => $this->input->post('title'),
'status' => 1,
'news_description' => $this->input->post('description'),
);
$this->User->update_news($file,$id);
$data = array('upload_data' => $this->upload->data());
redirect('admin_news_add');
}
}
You have two forms. First when you call form
<?php echo form_open_multipart('edit_news/update_news'); ?>
And inside foreach block
<form action="<?php echo base_url() . "edit_news/update_news/". $n->news_id; ?>"
method="post" class="form-horizontal" role="form">
which contain Button element. Because uploading need form attribute enctype set to multipart/form-data, your inner form does not have this so no file upload took place.
Try this
public function update_news()
{
$this->load->library('upload');
$config['upload_path'] = getcwd().'/assets/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '200000';
$config['max_width'] = '200000';
$config['max_height'] = '200000';
$config['overwrite'] = TRUE;
$this->upload->initialize($config);
$this->form_validation->set_rules('title', 'News Title', 'required|xss_clean');
$this->form_validation->set_rules('description', 'News Description', 'required|xss_clean');
if (!$this->upload->do_upload() || !$this->form_validation->run()) {
$error = array('error' => $this->upload->display_errors());
$this->load->view('your view name', $error);
} else {
$data = $this->upload->data();
$this->thumb($data);
$id = $this->uri->segment(3);
$file = array(
'img_name' => $data['raw_name'],
'thumb_name' => $data['raw_name'] . '_thumb',
'ext' => $data['file_ext'],
'news_date' => date("l , F j, Y "),
'title' => $this->input->post('title'),
'status' => 1,
'news_description' => $this->input->post('description'),
);
$this->User->update_news($file,$id);
$data = array('upload_data' => $this->upload->data());
redirect('admin_news_add');
}
}