I am trying to build a project with product information with image. I have tried some code given below, but I can not upload image path in the database. I am a newbie trying to learn code.
controller code:-
class Category extends CI_Controller{
public function __construct(){
parent::__construct();
$this->load->model('Category_model');
}
private function do_upload(){
$config['upload_path']='./uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 1000;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('product_image')){
$error = array('error' => $this->upload->display_errors());
$this->load->view('admin/pages/add_product_form', $error);
}
else
{
$upload_data = $this->upload->data();
$this->Category_model->insert_data( $upload_data['upload_path']);
}
$data = array('upload_data' => $this->upload->data());
}
function save_product(){
$this->do_upload();
$this->session->set_userdata('message','Product saved successfully');
$this->add_product();
}
}
Model code:-
function insert_data( $path_name){
$data = array(
'product_image' => $path_name
);
$data['product_status']=1;
$this->db->insert('tbl_product', $data);
}
view code:-
<form class="form-horizontal" action="<?php echo base_url()?>index.php/Category/save_product" enctype="multipart/form-data" method="post">
<fieldset>
<div class="control-group">
<label class="control-label" for="typeahead">Product Name </label>
<div class="controls">
<input type="text" name="product_name" class="span6 typeahead" id="typeahead" data-provide="typeahead" data-items="4" >
</div>
</div>
<div class="control-group">
<label class="control-label" for="selectError3">Product Category</label>
<div class="controls">
<select name="product_category" id="selectError3">
<option>Select Category</option>
<?php foreach($category_info as $category){ ?>
<option value="<?php echo $category->category_id ?>"><?php echo $category->category_name; ?></option>
<?php } ?>
<option>Option 2</option>
<option>Option 3</option>
<option>Option 4</option>
<option>Option 5</option>
</select>
</div>
</div>
<div class="control-group">
<label class="control-label" for="typeahead">Product Image </label>
<div class="controls">
<input name="product_image" type="file" name="fileToUpload" id="fileToUpload">
</div>
</div>
<div class="form-actions">
<button type="submit" class="btn btn-primary">Save changes</button>
<button type="reset" class="btn">Cancel</button>
</div>
</fieldset>
</form>
This is my code given above, but still not getting the image path in database, please help in this regard.Thanks in Advance.
Just increased max_size,max_width,max_height and these solved the problem.
Related
myiamge
I have 2 forms as tab in signup page when one form is submit and if have any error I want to redirect to that specific tab which have error but did not happen.
Here is my view
<?php include 'header.php'; ?>
<section class="services">
<div class="container">
<div class="signup-wrapper well" style="margin-bottom: 80px; margin-top: 80px;">
<div class="container">
<div class="services__main">
<h2 class="title title--main"><span class="title__bold">Sign Up</span><span class="line line--title"><span class="line__first"></span><span class="line__second"></span></span></h2>
</div>
<div class="aside-tabs__links">
Genrel User
Dealership
</div>
<div class="aside-tabs__blocks about-tab js-tab-block no-b-border" id="desc" >
<div class="col-md-5">
<form action="<?= base_url();?>Home/add_user" method="post" >
<!-- class="quick-form" -->
<div class="form-group">
<input type="text" class="form-control" name="fname" placeholder="Full Name" />
</div>
<?php echo form_error('fname'); ?>
<div class="form-group">
<input type="email" class="form-control" name="email" placeholder="Email" />
</div>
<?php echo form_error('email'); ?>
<div class="form-group">
<input type="password" class="form-control" name="password" placeholder="Password" />
</div>
<?php echo form_error('password'); ?>
<div class="form-group">
<input type="text" class="form-control" name="phone" placeholder="Phone #" />
</div>
<?php echo form_error('phone'); ?>
<div class="form-group">
<select class="select-2 form-control" name="city">
<option>Select City</option>
<?php foreach ($results as $result) { ?>
<option><?= $result->city; ?></option>
<?php } ?>
</select>
</div>
<?php echo form_error('city'); ?>
<div class="form-group">
<input type="submit" class="btn button button--red button--main pull-right" style="margin-bottom: 10px;" value="Sign Up">
</div>
</form>
</div>
</div>
<div class="aside-tabs__blocks about-tab js-tab-block no-b-border" id="rev" style="display: none;">
<div class="col-xs-5">
<form action="<?= base_url();?>Home/add_dealer" method="post" enctype="multipart/form-data">
<h4>Comapny Information</h4>
<hr>
<div class="form-group">
<input type="text" class="form-control" name="cname" placeholder="Company Name" />
</div>
<div class="form-group">
<input type="text" class="form-control" name="owner" placeholder="Owner Name" />
</div>
<div class="form-group">
<select class="select-2 form-control" name="city">
<option>Select City</option>
<?php foreach ($results as $result) { ?>
<option><?= $result->city; ?></option>
<?php } ?>
</select>
</div>
<div class="form-group">
<input type="text" class="form-control" name="address" placeholder="Office Location" />
</div>
<div class="form-group">
<input type="text" class="form-control" name="phone" placeholder="Office Phone" />
</div>
<div class="form-group">
<select name="business-type" class="form-control">
<option class="form-group">Products Deal</option>
<option class="form-group">Bikes</option>
<option class="form-group">Accessories</option>
<option class="form-group">Both</option>
</select>
</div>
<div class="form-group">
<input type="text" class="form-control" name="link" placeholder="social-link(optional)" />
</div>
<div class="form-group">
<button type="button" class="button button--custom--grey button--main btn" id="logo">Upload Logo</button>
<input type="file" class="form-control hidden" id="logo1" name="logo1" /><span id="mylogo">* Format must be jpg,jpeg or png</span>
</div>
<?php echo form_error('logo'); ?>
<div class="form-group">
<textarea class="form-control" name="descrp" placeholder="Description(optional)"></textarea>
</div>
<h4>Primary Information</h4>
<hr>
<div class="form-group">
<input type="text" class="form-control" name="email" placeholder="Email" />
</div>
<div class="form-group">
<input type="text" class="form-control" name="password" placeholder="Password" />
</div>
<div class="form-group">
<input type="submit" name="submit" class="btn button button--red button--main pull-right" value="Sign Up">
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</section>
<?php include 'footer.php'; ?>
here is my controller
function add_dealer()
{
//echo "<pre>"; print_r($_FILES);
$config['upload_path'] = 'C:\xampp\htdocs\devilbirds\images\uploads';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '0';
$config['max_width'] = '0';
$config['max_height'] = '0';
$config['encrypt_name'] = 'TRUE';
$config['overwrite'] = 'FALSE';
$config['wm_text'] = 'Deveil Birds';
$config['wm_type'] = 'text';
$config['wm_font_path'] = 'C:\xampp\htdocs\devilbirds/fonts/fontawesome-webfont.ttf';
$config['wm_font_size'] = '16';
$config['wm_font_color'] = 'aabbcc';
$config['wm_vrt_alignment'] = 'bottom';
$config['wm_hor_alignment'] = 'center';
$config['wm_padding'] = '20';
$this->load->library('upload', $config);
$this->image_lib->watermark();
$abc = $this->upload->do_upload('logo1');
$upload_data = $this->upload->data();
// var_dump($abc);exit();
$file_name = $upload_data['file_name'];
$this->form_validation->set_rules('cname','CName','trim|required');
$this->form_validation->set_rules('logo1','Logo1','trim');
$this->form_validation->set_rules('address','Address','trim');
$this->form_validation->set_rules('phone','Phone','trim|is_unique[bd_dealer.phone]');
$this->form_validation->set_rules('owner','Owner','trim');
$this->form_validation->set_rules('descrp','Descrp','trim');
$this->form_validation->set_rules('business-type','Business-type','trim');
$this->form_validation->set_rules('link','Link','trim');
$this->form_validation->set_rules('email','Email','trim|is_unique[bd_dealer.email]');
$this->form_validation->set_rules('password','Password','trim|min_length[5]|max_length[20]');
$this->form_validation->set_rules('city','City','trim');
if ($this->form_validation->run() == FALSE)
{
$data['error'] = $this->session->set_flashdata('errors');
$this->load->view('pages/signup',$data);// this is the link which loads my view i dont get how to go load that specific div from this url
}
else
{
$userData = array(
'company' => $this->input->post('cname'),
'logo' => $file_name,
'location' => $this->input->post('address'),
'phone' => $this->input->post('phone'),
'name' => $this->input->post('owner'),
'description' => $this->input->post('descrp'),
'business_type' => $this->input->post('business-type'),
'social_link' => $this->input->post('link'),
'email' => $this->input->post('email'),
'password' => $this->input->post('password'),
'city' => $this->input->post('city'));
$data = array(
'email' => $this->input->post('email'),
'password' => $this->input->post('password'),
'user_type' => '2');
//var_dump($userData);exit();
$this->Home_m->add_dealer($userData);
$this->Home_m->users($data);
redirect('Home/login');
}
}
I have two functions for two different forms in tabs. let suppose if i post my dealer form and this form have any issue then it will redirect to the signup page and it shows the pre-active tab which is general user signup. i want to redirect to the div that contain the form or dealer signup. any help will be appreciated , thanks in advance
In your controller when you get error do this.
for form one error
$data['error'] = $this->session->set_flashdata('errors');
$data['div1'] = "div1";
$this->load->view('pages/signup',$data);// this is the link which loads my view i dont get how to go load that specific div from this url ,i have pass div1 as parameter
For form two error
$data['error'] = $this->session->set_flashdata('errors');
$data['div2'] = "div2";
$this->load->view('pages/signup',$data);// this is the link which loads my view i dont get how to go load that specific div from this url ,i have pass div1 as parameter
In your view page
<script>
$(document).ready(function(){
var div1 = "<?php $div1; ?>";
var div2 = "<?php $div2; ?>";
if(div1=='' && div2==''){
$("#desc").show();
$("#rev").hide();
}else if(div1=='div1' && div2==''){
$("#desc").show();
$("#rev").hide();
}else if(div1=='' && div2=='div2'){
$("#desc").hide();
$("#rev").show();
}
})
</script>
div1
<div class="aside-tabs__blocks about-tab js-tab-block no-b-border" id="desc">
Div2
<div class="aside-tabs__blocks about-tab js-tab-block no-b-border" id="rev">
I hope it will help you.
I am new to Codeigniter and new to the MVC webdesign, i have plant insert form i want to upload image and save image name into database i tried many codes but not works please help me to write upload function
plant insert form is below here
<?php echo validation_errors(); ?>
<?php echo form_open('AddPlant/InsertPlant')?>
<div class="form-group has-error">
<label for="name">Name <span class="require">*</span></label>
<input type="text" class="form-control" name="name" />
</div>
<div class="form-group">
<label for="description">Description</label>
<textarea rows="5" class="form-control" name="description" >
</textarea>
</div>
<div class="form-group required">
<label for="exampleSelect1" class='control-label'>Job Type</label>
<select class="form-control" id="age" name="age">
<option value="1">Level 1</option>
<option value="2">Level 2</option>
<option value="3">Level 3</option>
</select>
</div>
<div class="form-group">
<p><span class="require">*</span> - required fields</p>
</div>
<div class="form-group">
<label for="description">Plant Image</label>
<input type="file" name="plantimg">
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">
Create
</button>
<button class="btn btn-default">
Cancel
</button>
</div>
<?php echo form_close();?>
My Codeigniter model
<?php
class Model_plants extends CI_Model
{
function insertPlantData(){
$data =array(
'name'=> $this->input->post('name',TRUE),
'description'=> $this->input->post('description',TRUE),
'age'=> $this->input->post('age',TRUE),
);
return $this->db->insert('plants',$data);
}
}
Hope this will help you :
Note : make sure you have load database and upload library either in autoload or in controller
First your form should be like this :
<?php echo form_open_multipart('AddPlant/InsertPlant')?>
............
<?php echo form_close();?>
Your controller should be like this :
public function InsertPlant()
{
/*here make sure your path is correct*/
$config['upload_path'] = FCPATH .'assets/uploads/';
$config['allowed_types'] = '*';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('plantimg'))
{
$error = array('error' => $this->upload->display_errors());
print_r($error);die;
}
else
{
//$data = array('upload_data' => $this->upload->data());
$file_name = $this->upload->data('file_name');
/*here assuming that your column name for image is image_name, change it not*/
$data =array(
'name'=> $this->input->post('name',TRUE),
'description'=> $this->input->post('description',TRUE),
'age'=> $this->input->post('age',TRUE),
'image_name'=> $file_name,
);
return $this->db->insert('plants',$data);
}
}
I have 2 websites in 1 htdocs directory. The structure's like this
The main directory it's structured like this(name:Quartiere):
Quartiere
_admin
application
assets
Second website directory it's _admin with the same structure(default CodeIgniter structure).
I'm trying to upload a file from _admin to the main directory assets folder, but for some reason, it's not working.I tried other answers from other post but nothing it helped.
What I tried:
Put 'userfile' in do_upload
Checked if the form it's multipart(it is)
What I checked:
If the path it's good(it is)
If the size of the photo it's smaller than 2048(it is)
_admin Controller:
public function add_product(){
if(!$this->session->userdata('logged_in')){
redirect('users/login');
}
$data['title'] = 'Adauga Produs';
$data['categories'] = $this->category_model->get_categories();
$this->form_validation->set_rules('name','Nume','required');
$this->form_validation->set_rules('category','Categorie','required');
$this->form_validation->set_rules('ingredients','Ingrediente');
$this->form_validation->set_rules('price','Pret','required');
$this->form_validation->set_rules('grams','Gramaj','required');
if($this->form_validation->run() === FALSE){
$this->load->view('templates/header');
$this->load->view('products/adauga', $data);
$this->load->view('templates/footer');
}else{
//upload image
$config['upload_path'] = 'http://localhost/quartiere/assets/images';
$config['allowed_types'] = 'jpg|png|jpeg';
$config['max_size'] = '2048';
$config['max_width'] = '2000';
$config['max_height'] = '2000';
$this->load->library('upload',$config);
if(!$this->upload->do_upload('userfile')){
$errors = array('error' => $this->upload->display_errors());
$food_image = 'noimage.png';
}else{
$data = array('upload_data' => $this->upload->data());
$food_image = $_FILES['userfile']['name'];
}
$this->food_model->add_product($food_image);
redirect('produse/adauga');
}
}
_admin View:
<?php echo validation_errors();?>
<?php echo form_open_multipart('products/add_product');?>
<div class="row">
<div class="col-md-4 col-md-offset-4">
<h1 class="text-center">
<?php echo $title; ?>
</h1>
<div class="form-group">
<input type="text" name="name" class="form-control" placeholder="Nume Produs" required/>
</div>
<div class="form-group">
<select class="form-control" name="category" required>
<?php foreach($categories as $category):?>
<option value="<?php echo $category['id'];?>"><?php echo $category['category_name'];?></option>
<?php endforeach;?>
</select>
</div>
<div class="form-group">
<label>Bar:</label><br>
Da <input type="radio" name="bar" class="from-control" value="1"/>
Nu <input type="radio" name="bar" class="from-control" value="0"/>
</div>
<div class="form-group">
<input type="text" name="ingredients" class="form-control" placeholder="Ingrediente"/>
</div>
<div class="form-group">
<input type="number" name="grams" class="form-control" placeholder="Gramaj"/>
</div>
<div class="form-group">
<label>Poza:</label>
<input type="file" name="userfile" size="10"/>
</div>
<div class="form-group">
<input type="number" name="price" class="form-control" placeholder="Pret" required/>
</div>
<button type="submit" class="btn btn-success btn-block">Adauga Produs</button>
</div>
</div>
<?php form_close(); ?>
You can make the upload path to - '../assets/images' - or - $_SERVER['DOCUMENT_ROOT'].'/quartiere/assets/images'. Either way is fine.
I am working on CI and i created one simple CRUD to insert PDF with some data in table. Sometimes my code works but now its completely not working.I am getting blank page after submitting my form. And nothing is happens like print_r and var_dump etc. I dont know whats wrong in my code.
My sidebar :
From where i click to my controller to get my view.
<li>Upload Pay slips</li>
Its working fine. On click this i am able to go to my page that is add_pay_slips.
Controller for the same:
public function add_pay_slips()
{
$this->load->view('layouts/header');
$this->load->view('layouts/sidebar');
$data['salary']=$this->pay_slips->getPaySlips1();
$this->load->view('responsibilities/add_pay_slips',$data);
$this->load->view('layouts/footer');
}
After this i get my form view:
<form method="post" enctype="multipart/form-data" class="form-horizontal" action="<?php echo base_url();?>responsible/upload">
<div class="control-group">
<label class="control-label">Select Month</label>
<div class="controls">
<select name="month">
<option value="1">January</option>
<option value="2">February</option>
<option value="3">March</option>
<option value="4">April</option>
<option value="5">May</option>
<option value="6">June</option>
<option value="7">July</option>
<option value="8">August</option>
<option value="9">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
</div>
</div>
<div class="control-group">
<label class="control-label">Select Year</label>
<div class="controls">
<select name="year">
<option>2017</option>
<option>2016</option>
<option>2015</option>
<option>2014</option>
<option>2013</option>
<option>2012</option>
<option>2011</option>
</select>
</div>
</div>
<div class="control-group">
<label class="control-label">Select Employee</label>
<div class="controls">
<select name="employee_name">
<?php
foreach ($salary as $row) {?>
<option value='<?php echo $row->EMPLOYEE_ID;?>'><?php echo $row->EMPLOYEE_NAME;}?></option>
<!--<input type="hidden" name="employee_id" value="<?php //echo $row->EMPLOYEE_ID?>">-->
</select>
</div>
</div>
<div class="control-group">
<label class="control-label">Select Country</label>
<div class="controls">
<select name="org_id">
<option value="40">UK</option>
<option value="65">INDIA</option>
<option value="47">POLAND</option>
<option value="57">GERMANY</option>
</select>
</div>
</div>
<div class="control-group">
<label class="control-label">Upload Payslip</label>
<div class="controls">
<input type="file" name="userfile"/>
</div>
</div>
<div class="form-actions">
<button type="submit" class="btn btn-success">Save</button>
</div>
</form>
and for this view on submit button i created one controller to insert data.
function upload(){
$filename = 'pdf';
$config = array(
'upload_path' => 'uploads',
'allowed_types' => "pdf",
'file_name' => $filename
);
$config['allowed_types'] = 'pdf';
$this->load->library('upload', $config);
$this->upload->initialize($config);
if($this->upload->do_upload())
{
$file_data = $this->upload->data();
$data1['userfile'] = $file_data['file_name'];
$data=
array(
'PAY_MONTH'=>$this->input->post('month'),
'PAY_YEAR'=>$this->input->post('year'),
'ORG_ID'=>$this->input->post('org_id'),
'EMPLOYEE_ID'=>$this->input->post('employee_name'),
'PDF'=>$data1['userfile'],
);
$this->load->model('pay_slips_model');
$result=$this->pay_slips->insert_payslips($data);
if($result==true)
{
$this->session->set_flashdata('msg',"PDF Added Successfully");
$this->load->view('layouts/header');
$this->load->view('layouts/sidebar');
$data['salary']=$this->pay_slips->getPaySlips1();
$this->load->view('responsibilities/add_pay_slips' ,$data,'refresh');
$this->load->view('layouts/footer');
}
else
{
$this->seesion->set_flashdata('msg1',"PDF Added Failed");
redirect('responsibilities/add_pay_slips');
}
}
}
And for this i have one model to insert data.
public function insert_payslips($data)
{
$this->db->insert('all_salary_slip_details',$data);
return ($this->db->affected_rows() != 1 ) ? false:true;
}
I have one page responsible.php my all controllers are on the same page.What is affecting my code. i am stuck here. Onclick Submit i just got my controller's url and blank page. payroll1/responsible/upload.
i am using codeigniter mvc framework, i want to make a form that has a upload picture and also save inputted data in input types in one html <form> why is this my image cannot be uploaded and it displays error like this when i echo the error:
File cannot be uploadedarray(1) { ["error"]=> string(43) "
You did not select a file to upload.
" }
Here is my code in view :
<form id="frm_product" class="form-horizontal" method="POST" action="Admin_controls/addProduct">
<div style="position:relative;">
<a class='btn btn-primary' href='javascript:;'>
Choose Picture..
<input type="file" style='position:absolute;z-index:2;top:0;left:0;filter: alpha(opacity=0);-ms-filter:"progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";opacity:0;background-color:transparent;color:transparent;' name="file_source" size="40" onchange='$("#upload-file-info").html($(this).val());'>
</a>
<span class='label label-info' id="upload-file-info"></span>
</div>
<br>
<div class="form-group">
<label for="cat_name">Product Name: </label>
<input type="text" class="form-control" name="prod_name">
</div>
<div class="form-group">
<label for="cat_desc">Description: </label>
<input type="text" class="form-control" name="prod_desc">
</div>
<div class="form-group">
<label for="cat_desc">Price: </label>
<input type="text" class="form-control" name="prod_price">
</div>
<div class="form-group">
<label for="cat_desc">Category: </label>
<select class="form-control" name="prod_cat">
<?php foreach($data as $each){ ?>
<option value="<?php echo $each->Category_id; ?>"><?php echo $each->Category_name; ?></option>';
<?php } ?>
</select>
</div>
</form>
Here is my javascript to submit the form:
function submitProduct() {
document.getElementById("frm_product").submit();
}
In my controller here is my code:
public function addProduct(){
$config['upload_path'] = './assets/uploaded_images/';
$config['allowed_types'] = 'jpg|jpeg|png';
$config['max_size'] = 1024 * 8;
$this->load->library('upload', $config);
if($this->upload->do_upload("file_source")) {
$upload_data = $this->upload->data();
$file_name = base_url().'assets/uploaded_images/'.$upload_data['file_name'];
$data = array (
'Product_name' => $this->input->post('prod_name'),
'Product_desc' => $this->input->post('prod_desc'),
'Product_price' => $this->input->post('prod_price'),
'Category_id' => $this->input->post('prod_cat'),
'Product_img' => $file_name
);
var_dump($data);
}
else {
echo "File cannot be uploaded";
$error = array('error' => $this->upload->display_errors()); var_dump($error);
}
}
Seems to me that you forgot to add enctype="multipart/form-data" to your form
<form id="frm_product" class="form-horizontal" method="POST" action="Admin_controls/addProduct" enctype="multipart/form-data">