controller
<?php
class Breaking extends MY_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('breaking_model');
}
public function create_breaking_news()
{
$this->securePage();
if ($this->input->post('submit')) {
$this->form_validation->set_rules('title', ' Breaking News Title', 'required');
$this->form_validation->set_rules('details', ' Breaking News Details', 'required');
if ($this->form_validation->run()) {
$added_on = time();
// if image is uploaded
if (isset($_FILES['userfile']['name']) && $_FILES['userfile']['size']>0) {
$config['upload_path'] = $this->config->item('upload_path');
$config['allowed_types'] = $this->config->item('allowed_types');
$config['max_size'] = $this->config->item('max_size');
$config['max_width'] = $this->config->item('max_width');
$config['max_height'] = $this->config->item('max_height');
$config['encrypt_name'] = $this->config->item('encrypt_name');
$this->load->library('upload', $config);
if ($this->upload->do_upload()) {
$image = $this->upload->data();
$this->breaking_model->add_news($image['file_name'], $added_on);
$this->session->set_flashdata('msg', '<button class="btn btn-success" style="width:100%;">The Breaking News has been added successfully.</button>');
redirect('breaking/manage-breaking-news');
} else {
$this->session->set_flashdata('msg', $this->upload->display_errors());
redirect('breaking/create-breaking-news');
}
} else {
// if image is not uploaded
$this->breaking_model->add_news(null, $added_on);
$this->session->set_flashdata('msg', '<button class="btn btn-success" style="width:100%;">The About Breaking News been added successfully.</button>');
redirect('breaking/manage-breaking-news');
}
}
}
$data['news_title'] = 'Add Breaking news | Kanchan news.com';
$data['Keywords'] = '';
$data['url'] = '';
$data['content'] = $this->load->view('breaking/create', $data, true);
$this->load->view('kanchan', $data);
}
}
my models
function add_news($image, $added_on) {
$this->db->set('title', $this->input->post('title'));
$this->db->set('details', $this->input->post('details'));
$this->db->set('video', $this->input->post('video'));
$this->db->set('images', $image);
$this->db->set('added_on', $added_on);
$this->db->insert('breaking_news');
}
and my view
<div class="container">
<ol class="breadcrumb">
<li class="active"><?php echo $this->session->userdata('full_name');?></li>
<li class="active">Breaking News</li>
<li class="active">Add Breaking News</li>
</ol>
<?php echo $this->session->set_flashdata('msg');?>
<form action="" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="title"> Enetr Breaking News Title :</label>
<hr>
<input type="text" name="title" class="form-control" value="<?php echo set_value('title');?>">
<?php echo form_error('title');?>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="title"> Enetr Breaking News Images :</label>
<hr>
<input type="file" name="userfile">
<?php echo form_error('userfile');?>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="title"> Enetr Breaking News Video Url( not required) :</label>
<hr>
<input type="video" name="video" class="form-control" value="<?php echo set_value('video');?>">
<?php echo form_error('video');?>
</div>
</div>
</div>
<div class="form-group">
<label for="title"> Enetr Breaking News Details :</label>
<hr>
<textarea class="ckeditor" name="details"><?php echo set_value('details');?></textarea>
<?php echo form_error('details');?>
</div>
<button class="btn btn-success" name="submit" value="submit" type="submit">Add Breaking News</button>
</form>
<hr>
</div>
i can add data without image successfully but when i select image it wont added data and redirect too create page without any error message and same problems with update too
Related
Been trying out codeigniter and wanted to create a form with file upload using the upload class. However when i try to save the form, it didn't respond and just show the same page. I've been looking around, but I can't seem to find out how to make this work.
This is the controller Product.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
Class Product extends CI_Controller{
function __construct(){
parent::__construct();
check_not_login();
$this->load->model('product_m');
}
public function index(){
$data['row'] = $this->product_m->get();
$this->template->load('template', 'product/data/product_data', $data);
}
public function add(){
$product = new stdClass();
$product->product_id = null;
$product->product_name = null;
$product->customer_name = null;
$product->supplier_name = null;
$product->weight = null;
$product->product_date = null;
$product->expired_date = null;
$product->image = null;
$product->barcode = null;
$data = array(
'page' => 'add',
'row' => $product
);
$this->template->load('template', 'product/data/product_form', $data);
}
public function edit($id){
$query = $this->product_m->get($id);
if($query->num_rows()>0){
$product = $query->row();
$data = array(
'page' => 'edit',
'row' => $product
);
$this->template->load('template', 'product/data/product_form', $data);
}else{
echo "<script>alert('Data not found');";
echo "window.location='".site_url('product')."';</script>";
}
}
public function process(){
$post = $this->input->post(null, TRUE);
if(isset($_POST['add'])){
$config['upload_path'] = './uploads/product';
$config['allowed_types'] = 'jpeg|pdf|jpg|png|doc|docs|xls|xlsx';
$config['max_size'] = 5120;
$config['file_name'] = 'product-'.date('ymd').'-'.substr(md5(rand()),0,10);
$this->load->library('upload', $config);
if(#$_FILES['image']['name'] !=null){
if($this->upload->do_upload('image')){
$post['image'] = $this->upload->data('file_name');
$this->product_m->add($post);
if($this->db->affected_rows() > 0){
$this->session->set_flashdata('success', 'Data saved successfully');
}
redirect('product');
}else{
$error = $this->upload->display_errors();
$this->session->set_flashdata('error', $error);
redirect('product/add');
}
}
else{
$post['image'] = null;
$this->product_m->add($post);
if($this->db->affected_rows() > 0){
$this->session->set_flashdata('success', 'Data saved successfully');
}
redirect('product');
}
}else if(isset($_POST['edit'])){
$this->product_m->edit($post);
}
}
public function del($id){
$this->product_m->del($id);
if($this->db->affected_rows() > 0){
$this->session->set_flashdata('Success', 'Data successfully deleted');
}
redirect('product');
}
}
Script for the form product_form.php
<section class="content-header">
<h1>
Product
<small>Add product</small>
</h1>
</section>
<section class ="content">
<div class="box">
<div class="box-header">
<h3 class="box-title"> product</h3>
<div class="pull-right">
<a href="<?=site_url('product')?>" class="btn btn-warning btn-flat">
<i class="fa fa-undo"></i> Back
</a>
</div>
</div>
<div class="box-body">
<div class="row">
<div class="col-md-4 col-md-offset-4">
<?php echo form_open_multipart('product/process');?>
<div class="form-group">
<label>Product Name *</label>
<input type="hidden" name="id" value="<?=$row->product_id?>">
<input type="text" name="product_name" value="<?=$row->product_name?>" class="form-control" required>
</div>
<div class="form-group">
<label>Customer Name *</label>
<input type="text" name="customer_name" value="<?=$row->customer_name?>" class="form-control" required>
</div>
<div class="form-group">
<label>Supplier Name *</label>
<input type="text" name="supplier_name" value="<?=$row->supplier_name?>" class="form-control" required>
</div>
<div class="form-group">
<label>Weight(kg) *</label>
<input type="number" name="weight" value="<?=$row->weight?>" class="form-control" required>
</div>
<div class="form-group">
<label>Product Date(dd/mm/yyyy) *</label>
<input type="text" name="product_date" value="<?=$row->product_date?>" class="form-control" required>
</div>
<div class="form-group">
<label>Expired Date(dd/mm/yyyy) *</label>
<input type="text" name="expired_date" value="<?=$row->expired_date?>" class="form-control" required>
</div>
<div class="form-group">
<label>Technical Data</label>
<input type="file" name="image" class="form-control" >
</div>
<!-- FORM BARCODE -->
<div class="form-group">
<label>Barcode</label>
<input name="barcode" class="form-control" value="
<?php
$angka = rand(1000000,9999999);
$random = rand(0,25);
$random1 = rand(0,25);
$random2 = rand(0,25);
$name = array("A", "B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q",
"R","S","T","U","V","W","X","Y","Z");
echo $name[$random],$name[$random1],$name[$random2],$angka;
$row->barcode
?>" readonly>
</div>
<div class="form-group">
<button type="submit" name="<?=$page?>" class="btn btn-success btn-flat">
<i class="fa fa-paper-plane"> Save</i>
</button>
<button type="Reset" class="btn btn-flat"> Reset</button>
</div>
<!-- </form> -->
<?php echo form_close();?>
</div>
</div>
</div>
</div>
</section>
Load the form helper.
function __construct(){
parent::__construct();
check_not_login();
$this->load->model('product_m');
$this->load->helper('form');
}
I'm trying to insert data into database through a form. I'm uploading 2 image files, in database path of the files will be stored. I'm trying to do server side validation. when user does not upload a file or when user is uploading wrong file type, an error messages should display and page should return to same page with no data inserted into database.
This is my view:
<div class="main-panel">
<div class="content-wrapper">
<div class="row">
<div class="col-md-12 grid-margin stretch-card">
<div class="card">
<div class="card-body">
<h4 class="card-title">Add News & Event</h4>
<p class="card-description"> </p>
<?php echo validation_errors("<div class='alert alert-danger'>","</div>");?>
<?php if($failed = $this->session->flashdata('addNewsFailed')){
echo '<div class="alert alert-danger">' ;
echo $failed;
echo '</div>';
}?>
<form class="forms-sample" action="" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="exampleInputName1">Title</label>
<input type="text" class="form-control" id="exampleInputName1" value="<?php echo
set_value("title");?>" name="title">
</div>
<div class="form-group">
<label for="exampleInputName1">Sub Title</label>
<input type="text" class="form-control" id="exampleInputName1" value="<?php echo set_value("sub");?>" name="sub_title">
</div>
<div class="form-group">
<label for="exampleInputName1">Event Date</label>
<input type="text" class="form-control" id="exampleInputName1" name="date" value="<?php echo date('d - M - y ');//echo set_value("date");?>">
</div>
<div class="container">
<div class="row">
<div class="col-md">
<div class="form-group">
<label>Feature Image</label>
<div class="input-group col-xs-12">
<input type="file" class="form-control file-upload-info" placeholder="Upload Image" name="img" style="z-index: 9999; opacity: 0;" id="image">
<div id="imageName"></div>
<span class="input-group-append">
<button class="file-upload-browse btn btn-info" type="button" style="position: absolute; left: 0; padding: 6px 23px;border-radius:3px;">Upload</button> </span>
</div>
<?php if(isset($upload_error)){
echo $upload_error;
}?>
</div>
</div>
<div class="col-md">
<div class="form-group">
<label> Banner Image</label>
<div class="input-group col-xs-12">
<input type="file" class="form-control file-upload-info" placeholder="Upload Image" name="banner" id="banner" style="opacity: 0; z-index: 9999;">
<div id="bannerName"></div>
<span class="input-group-append">
<button class="file-upload-browse btn btn-info" type="button" style="position: absolute;left: 0;padding: 6px 23px;border-radius: 3px;">Upload</button></span>
</div>
<?php if(isset($upload_error1)){
echo $upload_error1;
}?>
</div>
</div>
</div>
</div>
<span class="input-group-append">-->
<div class="form-group">
<label for="exampleTextarea1">Text</label>
<textarea class="form-control" id="exampleTextarea1" rows="2" name="para" value=""><?php echo set_value("para");?></textarea>
</div>
<button type="submit" class="btn btn-success mr-2">Add</button>
<button class="btn btn-light">Cancel</button>
</form>
</div>
</div>
</div>
</div>
</div>
This is my controller:
public function addEvent()
{
//load library
$this->load->library('form_validation');
$this->load->helper('form');
//set rules for validation
$this->form_validation->set_rules("title", "Title", "required");
$this->form_validation->set_rules("date", "Event Date", "required");
$this->form_validation->set_rules("para", "Text", "required");
//template
$this->output->set_template('admin_layout');
//image upload
$config = [
'upload_path' => './uploads/',
'allowed_types' => 'gif|jpg|png',
];
$this->load->library('upload', $config);
//validation
if ($this->form_validation->run()) {
$post = $this->input->post();
if (!$this->upload->do_upload('img')) {
$upload_error = $this->upload->display_errors();
$this->load->view('dashboard/pages/forms/addEvent', compact('upload_error'));
} else {
$Feature1 = $this->upload->data();
$image_path = base_url('uploads/' . $Feature1['raw_name'] . $Feature1['file_ext']);
$post['image_path'] = $image_path;
}
if (!$this->upload->do_upload('banner')) {
$upload_error1 = $this->upload->display_errors();
$this->load->view('dashboard/pages/forms/addEvent', compact('upload_error1'));
} else {
$carsousel = $this->upload->data();
$banner = base_url('uploads/' . $carsousel['raw_name'] . $carsousel['file_ext']);
$post['banner'] = $banner;
}
$this->load->model('loginmodel', 'addNews');
$insert_id = $this->addNews->addNews($post);
if ($insert_id) {
$this->session->set_flashdata("addNewsSuccess", 'News & Event Added Successfully');
redirect('dashboard/update_news/' . $insert_id);
} else {
$this->session->set_flashdata("addNewsFailed", 'Failed');
$this->load->view('dashboard/pages/forms/addEvent');
}
} else {
$upload_error = $this->upload->display_errors();
$this->load->view('dashboard/pages/forms/addEvent', compact('upload_error'));
}
}
This is my model:
public function addNews($array)
{
$this->db->insert('news', $array);
return $this->db->insert_id();
}
My form validation are working perfectly but when user is uploading wrong file type or does not upload any file then file upload error message is not displaying and data is inserting into database the with both image field empty.
I am unable to upload a image in the destination folder in codeigniter, I created one controller under the controller/admin folder with a name called site. And I added one model under the model folder with a name called model_view, and finally I created one view in the view folder with a name called view_addsite. And I created one table in the mysql with a name called sitesettings and 14 columns are added in that table. Problem hear is all fields are added in the table except image. please find the code.
controller(site)::
===========
public function add()
{
$datte = date('Y-m-d H:i:s');
if(!$this->input->post('buttonSubmit'))
{
$data['message'] = '';
$this->load->view('admin/view_addsite', $data);
}
else
{
//$this->load->library('form_validation');
if($this->form_validation->run('addsite'))
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_width'] = '2048';
$config['max_height'] = '2048';
$this->load->library('upload', $config);
$title = $this->input->post('title');
$name = $this->input->post('name');
$admin = $this->input->post('admin');
$mail = $this->input->post('mail');
$phone = $this->input->post('phone');
$contact=$this->input->post('contact');
$fb=$this->input->post('fb');
$tw=$this->input->post('tw');
$yt=$this->input->post('yt');
$in=$this->input->post('in');
$gp=$this->input->post('gp');
$ft=$this->input->post('ft');
$this->upload->do_upload('image');
$data = $this->upload->data('image');
$image= $data['file_name'];
$this>model_site>
insert($title,$image,$name,$admin,$mail,
$phone,$contact,$fb,$tw,$yt,$in,$gp,$ft);
$this->session->set_flashdata('message','site Successfully
Created.');
redirect(base_url('admin/site'));
}
else
{
$data['message'] = validation_errors();
$this->load->view('admin/view_addsite', $data);
}
}
}
Model(model_site)::
===================
public function
insert($title,$image,$name,$admin,$mail,$phone,$contact,
$fb,$tw,$yt,$in,$gp,$ft)
{
$data = array(
'admintitle' => $title,
'logo' => $image,
'fromname' => $name,
'adminemail'=> $admin,
'receivemail' => $mail,
'phonenumber' => $phone,
'contactaddress' => $contact,
'facebook' => $fb,
'twitter'=>$tw,
'youtube' => $yt,
'instagram' => $in,
'googleplus' => $gp,
'footer' => $ft,
);
$this->db->insert('sitesettings', $data);
}
view(view_addsite)::
===================
<!-- page content -->
<div class="right_col" role="main">
<div class="">
<div class="page-title">
<div class="title_left">
<h3>Sitesettings</h3>
</div>
</div><div class="clearfix"></div>
<hr>
<div class="row">
<div class="col-md-12 col-sm-12 col-xs-12">
<div class="x_panel">
<div class="x_title">
<h2>Add a new Site</h2>
<ul class="nav navbar-right panel_toolbox">
<li><a class="collapse-link"><i class="fa fa-
chevron-up"></i></a></li>
<li><a class="close-link"><i class="fa fa-close">
</i></a></li>
</ul>
<div class="clearfix"></div>
</div>
<div class="x_content">
<label><?php echo $message; ?></label>
<form method="post">
<fieldset>
<div class="form-group">
AdminPage Title : <input class="form-control"
placeholder="Admin Title" name="title" id="title"
type="text" ><span id="user-availability-status">
</span>
<div class="form-group">
Select image to upload:
<input type="file" name="fileToUpload"
id="fileToUpload">
</form>
<div class="form-group">
From Name : <input class="form-control"
placeholder="Form Title" name="name" id="name" type="text"
><span id="user-availability-status"></span>
<div class="form-group">
Admin Email Address : <input class="form-control"
placeholder="Admin Email" name="admin" id="admin"
type="text" onBlur="checkAvailability()" ><span
id="user-availability-status"></span>
<div class="form-group">
Receive Mail Address for Contact Us Form : <input
class="form-control" placeholder="Receive Email"
name="mail" id="mail" type="text"
onBlur="checkAvailability()" ><span id="user-availability-
status"></span>
</div>
<div class="form-group">
Phone number: <input class="form-control"
placeholder="Phone Number" name="phone" id="phone"
type="text" onBlur="checkAvailability()" ><span id="user-
availability-status"></span>
</div>
<div class="form-group">
Contact Adress : <input class="form-control"
placeholder="Contact Address" name="contact" id="contact"
type="text" onBlur="checkAvailability()" ><span
id="user-availability-status"></span>
</div>
<div class="form-group">
Facebook : <input class="form-control" placeholder="Facebook"
name="fb" id="fb" type="text" onBlur="checkAvailability()" >
<span id="user-availability-status"></span>
</div>
<div class="form-group">
Twitter : <input class="form-control" placeholder="Twitter"
name="tw" id="tw" type="text" onBlur="checkAvailability()" >
<span id="user-availability-status"></span>
</div>
<div class="form-group">
Youtube : <input class="form-control"
placeholder="Youtube" name="yt" id="yt" type="text"
onBlur="checkAvailability()" ><span id="user-availability-
status"></span>
</div>
<div class="form-group">
Instagram : <input class="form-control"
placeholder="Instagram" name="in" id="in" type="text"
onBlur="checkAvailability()" ><span id="user-availability-
status"></span>
</div>
<div class="form-group">
Google Plus: <input class="form-control"
placeholder="Google Plus" name="gp" id="gp" type="text"
onBlur="checkAvailability()" ><span id="user-availability-
status"></span>
</div>
<div class="form-group">
Footer: <input class="form-control"
placeholder="Footer" name="ft" id="ft"
type="text" onBlur="checkAvailability()" ><span id="user-
availability-status"></span>
</div>
<input type="submit" name="buttonSubmit"
value="add" class="btn btn-success" />
</fieldset>
</form>
</div> <!-- /content -->
</div><!-- /x-panel -->
</div> <!-- /col -->
</div> <!-- /row -->
</div>
</div> <!-- /.col-right -->
<!-- /page content -->
<?php $this->load->view('admin/partials/admin_footer'); ?>
<?php if($this->session->flashdata('message') != NULL) : ?>
<script>
swal({
title: "Success",
text: "<?php echo $this->session->flashdata('message'); ?>",
type: "success",
timer: 1500,
showConfirmButton: false
});
</script>
<?php endif ?>
public function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 100;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
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());
$this->load->view('upload_success', $data);
}
}
for more detail see:upload file with codeigniter
You are missing
enctype="multipart/form-data"
on your form tag and also remove the </form> just after the image input box.
Controller:
$doc_1 = "uploads/img_1/";
if (isset($_FILES['userfile_1']) && !empty($_FILES['userfile_1'])) {
$filename = pathinfo($_FILES['userfile_1']['name']);
if (move_uploaded_file($_FILES['userfile_1']['tmp_name'], $doc_1.$filename['basename'])) {
$attch_1 = base_url().$doc_1.$filename['basename'];
}
else {
$attch_1 = "na";
}
}
view:
<?php echo form_open_multipart("store_property", ['class'=>'form-horizontal']); ?>
<input type="file" name="userfile_1" />
I have two fields one for link and one for file upload in form, how do i add only one of those i.e. either link or file in database,
Sorry for any wrong code and methods, I am a newbie here and this is my first question in stack overflow. Hope you all help me out.
My Controller is
public function add_scrolllink()
{
$this->form_validation->set_rules('stitle','File Title','trim|required');
$this->form_validation->set_rules('slink','File Link','trim|required');
if(empty($_FILES['sfile']['name']))
{
$this->form_validation->set_rules('slink','File Link','required');
}
if($this->form_validation->run() == FALSE)
{
$this->load->view("add_scrolllink");
}
else
{
$slink_id = $this->input->post('stitle');
$slink_files = $this->input->post('slink');
$config['upload_path'] = './assets/uploads/';
$config['allowed_types'] = 'pdf|docx|doc';
$config['max_size'] = 9000;
$this->upload->initialize($config);
$this->load->library('upload',$config);
if(!$this->upload->do_upload('slink'))
{
$msg1 = $this->upload->display_errors();
header("Location:".base_url()."news/add_scrolllink?msg1=$msg1");
}
else
{
$output = $this->upload->data();
$file2 = $output['file_name'];
$this->load->model('mpages');
$output1 = $this->mpages->add_scrolllink($slink_title,$file2);
if($output1)
{
$msg = "Scrolling Link added successfully!";
header("Location:".base_url()."news/scrolllink_list?
msg=$msg");
}
else
{
$msg1 = 'Scrolling Link add failed. Try again!';
header("Location:".base_url()."news/add_scrolllink?
msg1=$msg1");
}
}
}
//My View is
<form action="<?php echo base_url(); ?>news/add_scrolllink" method="post"
role="form" enctype="multipart/form-data">
<div class="row">
<div class="col-md-2"></div>
<div class="col-md-8">
<div class="panel panel-default">
<div class="panel-body">
<div class="form-group">
<label>Scrolling Link Title</label>
<input type="text" name="stitle" class="form-
control" value="<?php echo set_value('stitle'); ?>" placeholder="Enter
Scrolling Link Title">
</div>
<style type="text/css">
p.help-block{color:#ff0000;}
</style>
<div class="form-group">
<label>Scrolling Link</label>
<input type="text" name="slink" class="form-control"
value="<?php echo set_value('slink'); ?>" placeholder="Enter Scrolling
Link">
</div>
OR ADD FILE TO SCROLL LINK
<div class="form-group">
<label>Choose File to Upload</label>
<input type="file" name="slink" class="form-
control">
</div>
<input type="submit" name="submit" class="btn btn-
primary" value="Add Link"><br>
<?php echo "<br><span
style='color:#ff0000;'>".validation_errors()."</span>"; ?>
<?php
if(isset($_GET['msg'])){
echo "<div class='alert alert-success
fadein'>".$_GET['msg']."</div>";
}
if(isset($_GET['msg1'])){
echo "<div class='alert alert-danger
fadein'>".$_GET['msg1']."</div>";
}
?>
</div>
</div>
</div>
<div class="col-md-2"></div>
<div class="clearfix"></div>
</div>
</form>
you have use jquery to get input box value using this code and set some condition
$('#slink-id').val(); // set link field input box id
$('#slinkFile-id').val(); //set upload file field id
if($('#slink-id').val() != '' && $('#slinkFile-id').val() == ''){
// setread only your file field
}else{
// set read only link field
}
This is my controller file that receives input from view file. post data will be submited to Add function and the function manipulates do upload function which recieves configuration parameters from set_configuration_function.
public function __construct()
{
parent::__construct();
$this->load->model('App/AppModel');//load app model
$this->load->helper('url');
$this->load->helper('form');
$this->load->library('session');
}
public function index()
{
$this->data['blogs'] = $this->AppModel->get_all(BLOGS_TABLE);
parent::LOADER('App/Blog/All');
}
public function Add()
{
if ($_POST && empty($_FILES['blog_file']['name'])) {
$new_blog = $_POST;
$this->AppModel->insert(BLOGS_TABLE, $new_blog);
$this->session->set_flashdata('flashSuccess', 'Blog Created Successfully.');
$this->index();
}
if ($_POST && !empty($_FILES['blog_file']['name'])) {
$file_name =$this->do_upload();
$file_name = array($file_name);
$blog = $_POST;
$new_blog = array_merge($blog, $file_name);
$this->AppModel->insert(BLOGS_TABLE, $new_blog);
$this->session->set_flashdata('flashSuccess', 'Blog Created Successfully.');
$this->index();
}
parent::LOADER('App/Blog/New');
}
function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}
}
public function set_upload_options (){
$config = array();
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '10000000000';
$config['max_width'] = '1024';
$config['max_height'] = '768';
return $config;
}
This is my View File
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
<!-- Content Header (Page header) -->
<section class="content-header col-md-offset-1">
<h1>
<?php echo BLOG_HEADING; ?>
<small><?php echo ADD; ?></small>
</h1>
</section>
<!-- Main content -->
<section class="content">
<div class="row">
<!-- right column -->
<div class="col-md-8 col-md-offset-1">
<!-- general form elements disabled -->
<div class="box box-warning">
<div class="box-header with-border">
<h3 class="box-title"><?php echo ADD; ?> Blog</h3>
</div>
<!-- /.box-header -->
<?php echo form_open_multipart('App/Blog/Add');?>
<div class="box-body">
<!-- text input -->
<!-- input states -->
<div class="form-group has-success">
<label class="control-label" for="inputSuccess"> <?php echo BLOG_TITLE; ?> *</label>
<input type="text" name="blog_title" class="form-control" id="inputSuccess" placeholder="Enter Title" required>
</div>
<div class="form-group has-success">
<label class="control-label" for="inputSuccess"> <?php echo BLOG_AUTHOR; ?> *</label>
<input type="text" name="blog_author" class="form-control" id="inputSuccess" placeholder="Enter Author" required>
</div>
<div class="form-group has-success">
<label class="control-label" for="inputSuccess"> <?php echo BLOG_URL; ?></label>
<input type="text" name="blog_url" class="form-control" id="inputSuccess" placeholder="Add Link" required>
</div>
<!-- textarea -->
<div class="form-group has-success">
<label class="control-label" for="inputSuccess"><?php echo BLOG_CONTENT; ?> *</label>
<textarea name="blog_content" class="form-control" rows="3" placeholder="Enter Content" required></textarea>
</div>
<div class="form-group has-success">
<label for="exampleInputFile">Blog Image</label>
<input type="file" name="blog_file" id="exampleInputFile">
</div>
</div>
<!-- /.box-body -->
<div class="box-footer">
<button type="submit" class="btn btn-primary"><?php echo SAVE; ?></button>
</div>
</form>
</div>
</div>
</section>
</div>
Problem is I get error that says "you did not select a file to upload."
you are not using set_upload_options() method.
you are missing input name blog_file in your controller.
$this->upload->do_upload('blog_file');