Can't upload images in Codeigniter - image

I have a problem with upload images in Codeigniter, when I add a new image an error show ( You did not select a file to upload.) I dont know if miss something in my code
this my function controller:
public function upload_img(){
$this->load->model('esthetique_model');
$config['upload_path'] = realpath(APPPATH.'../upload');
$config['allowed_types'] = 'jpg|jpeg|png|gif';
$config['max_size'] = '204800';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload()){
$error = array('error' => $this->upload->display_errors());
foreach ($error as $item => $value){
echo'<ol class="alert alert-danger"><li>'.$value.'</ol></li>';
}
exit;
}else{
$upload_data = $this->upload->data();
$path = $upload_data['file_name'];
$this->esthetique_model->photo_insert_mdl($path);
echo'<h4 style="color:green">Image uploaded Succesfully</h4>';
}
}
my view :
<?php echo form_open('esthetique/upload_img'); ?>
<div class="modal-body">
<!-- hidden input montinned with class sr-only -->
<div class="form-group"><label class="sr-only" =""></label>
<input type="text" name="idsc" class="sr-only" id="idsc" ></div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="userfile">Choisissez l'image</label>
<input type="file" name="userfile" id="userfile">
</div>
</div>
<div class="form-group">
<img id="loader" src="<?php echo base_url() ?>asset/images/486.GIF" style="height: 30px;">
</div>
<div class="form-group">
<img id="preview" src="#" style="height: 80px;border: 1px solid #DDC; " />
</div>
</div>
<div class="form-group ">
<div class="input-group">
<div class="input-group-addon">
<i class="fa fa-calendar">
</i>
</div>
<input class="form-control" id="date" name="date" placeholder="Date" type="text"/>
</div>
</div>
<div class="form-group">
<textarea class="form-control" row="3" id="note" name="note" placeholder="Note"></textarea>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Fermer</button>
<button type="submit" class="btn btn-primary">Enregistrer</button>
</div>
<?php echo form_close(); ?>
</div>
</div>
</div>
thanks for helping

Try the following (specifying the file input name in your controller):
...
if ( ! $this->upload->do_upload('userfile')) {
...
More information that may be useful: http://www.codeigniter.com/user_guide/libraries/file_uploading.html

I have changed in view
<?php echo form_open('esthetique/upload_img'); ?>
by
<?php echo form_open_multipart('esthetique/upload_img'); ?>
its work now

Related

How can fixed the codeigniter cart update issue

I am unable to update cart on checkout page. I do not know what is Happen.
Here is my code.
function update_cart()
{
$rowid =$this->input->post('rowid');
$price =$this->input->post('price');
$qty = $this->input->post('qty');
$pr = preg_replace( '/,/', '', $price);
$amount = $pr * $qty;
$data = array(
'rowid' => $rowid,
'price' => $price,
'amount' => $amount,
'qty' => $qty
);
$this->cart->update($data);
redirect('checkout');
}
Here is my car view code .I am trying to update cart value here.No error showing. I unable to solve .Any help .Only update cart is not working . Other functionality is working good .add to cart delete to cart but update to cart does not working . I create session table and also try other suggestion and solution form stack overflow.
<form action="<?=base_url()?>cart/update_cart" method="post">
<div class="panel panel-info">
<div class="panel-heading">
<div class="panel-title">
<div class="row">
<div class="col-xs-6">
<h5><span class="glyphicon glyphicon-shopping-cart"></span> Shopping Cart</h5>
</div>
<div class="col-xs-6">
<a href="<?=base_url()?>">
<button type="button" class="btn btn-primary btn-sm btn-block">
<span class="glyphicon glyphicon-share-alt"></span> Continue shopping
</button>
</a>
</div>
</div>
</div>
</div>
<div class="panel-body">
<?php $i = 1; ?>
<?php
//print_r($this->cart->contents() );
//die;
foreach ($this->cart->contents() as $items): ?>
<?php echo form_hidden($i.'[rowid]', $items['rowid']); ?>
<div class="row">
<div class="col-xs-2">
<img class="img-responsive" src="<?=base_url()?>uploads/product/<?=$items['image'];?>" />
</div>
<div class="col-xs-4">
<h5 class="product-name">
<strong><?php echo $items['name']; ?>
</strong>
<?=$items['options'];?>
</h5>
</div>
<div class="col-xs-6">
<div class="col-xs-6 text-right">
<h6><strong><?= $this->cart->format_number($items['price']); ?>
<span class="text-muted">x</span></strong></h6>
</div>
<div class="col-xs-4">
<?php //echo form_input(array('name' => $i.'[qty]', 'value' => $items['qty'], 'size' => '1')); ?>
<input type="text" name="qty" min="1" value="<?=$items['qty'] ?>" maxlength="1" style="width: 50%;">
<input type="hidden" name="price" value="<?=$items['price'] ?>">
<input type="hidden" name="name" value="<?=$items['name'] ?>">
<input type="hidden" name="description" value="<?=$items['description'] ?>">
</div>
<div class="col-xs-2">
<a href="<?=base_url()?>cart/remove/<?=$items['rowid']?>">
<button type="button" class="btn btn-link btn-xs">
<span class="glyphicon glyphicon-trash" onclick="confirm('Are you sure want to delete')";> </span>
</button>
</a>
</div>
</div>
</div>
<hr>
<?php $i++; ?>
<?php endforeach; ?>
at view .... use only two input
<input type="text" name="qty[]" min="1" value="<?=$items['qty'] ?>" maxlength="1" style="width: 50%;">
<input type="hidden" name="rowid[]" value="<?=$items['rowid'] ?>">
at controller
you only need to change qty if you are going to update a cart
function update_cart()
{
for ($i=0; $i < count($_POST) - 1; $i++) {
$data = array(
'rowid' => $_POST['rowid'][$i],
'qty' => $_POST['qty'][$i]
);
$this->cart->update($data);
}
redirect('checkout');
}

updating image hasfile condition

Now i'm trying to update an image but in method update it keeps skip the hasfile condition
public function update(Request $request, $id)
{
$slider = Slider::find($id);
$slider->header = $request->header;
$slider->paragraph=$request->paragraph;
if($request->hasFile('image')){
return 'a';
// $image=$request->file('image');
// $filename=time(). '.' .$image->getClientOriginalExtension();
// $location=public_path('images/' . $filename);
// Image::make($image)->save($location);
// $oldFilename=$slider->image;
// $slider->image=$filename;
// File::delete(public_path('images/'. $oldFilename));
}else{
return 'whatever';
}
}
and here's my view
<form class="form-horizontal" action="{{ route('slider.update',$slider->id) }}" method="post" enctype="multipart/form-data">
{{ csrf_field() }}
{{method_field('PATCH')}}
<div class="row">
<div class="col-lg-12">
<div class="ibox float-e-margins">
<div class="ibox-title back-change">
<h5>الغلاف </h5>
</div>
<div class="ibox-content">
<div class="row">
<div class="col-md-6">
<div class="image-crop">
<img src="{{asset('images/'.$slider->image)}}">
</div>
</div>
<div class="col-md-6">
<div class="btn-group">
<label title="Upload image file" for="inputImage" class="btn btn-primary">
<input type="file" name="image" id="inputImage" class="hide">
Upload new image
</label>
</div>
</div>
<div class="form-group">
<div class="col-lg-offset-2 col-lg-10">
<button class="btn btn-primary pull-right" type="submit"> حفظ التغيرات</button>
</div>
</div>
</div>
</div>
</div>
Why can I not get to the condition?
The form is okay and the name of the input is okay, but it still returns to else.

why does $this->form_validation->run() always return false in codeigniter 3.0.0 with php7.1 on ubuntu 16.04?

This is part of the code, it made me cannot login, i don't know where is the problem. When i tried to log in, it has no effect. Sorry for my bad english.
public function login()
{
if ($this->identity->is_admin())
redirect('admin/dashboard');
if ($this->identity->is_contestant())
redirect('contestant/dashboard');
$this->form_validation->set_rules('form[username]', $this->lang->line('username'), 'trim|required|max_length[50]');
$this->form_validation->set_rules('form[password]', $this->lang->line('password'), 'trim|required|max_length[50]');
if ($this->form_validation->run())
{
$credentials = $this->input->post('form');
if ($this->identity->login($credentials))
{
if ($this->identity->is_admin())
redirect('admin/dashboard');
else
redirect('contestant/dashboard');
}
else
{
$this->session->set_flashdata('error', $this->lang->line('wrong_credentials'));
redirect('site/login');
}
}
else
{
$this->ui['header']['title'] = $this->lang->line('login');
$this->ui['header']['page'] = 'login';
$this->load->view('site/header', $this->ui['header']);
$this->load->view('site/login', $this->ui['content']);
$this->load->view('footer', $this->ui['footer']);
}
}
Here are the login site source code.
<div class="container">
<div class="row">
<div class="col-md-12">
<ul class="breadcrumb">
<li>
<i class="glyphicon glyphicon-off"></i> <?php echo $this->lang->line('please_login'); ?>
</li>
</ul>
</div>
</div>
<?php if ($this->session->flashdata('error')) : ?>
<div class="row">
<div class="col-md-5">
<div class="alert alert-danger">
<?php echo $this->session->flashdata('error'); ?>
</div>
</div>
</div>
<?php endif; ?>
<div class="row">
<div class="col-md-12">
<form class="form-horizontal" action="" method="post">
<div class="form-group<?php echo form_error('form[username]') ? ' has-error' : ''; ?>">
<label class="col-sm-1 control-label"><?php echo $this->lang->line('username'); ?>:</label>
<div class="col-sm-4">
<input name="form[username]" type="text" maxlength="30" class="form-control" value="<?php echo set_value('form[username]'); ?>" autofocus="on"/>
<span class="help-block"><?php echo form_error('form[username]'); ?></span>
</div>
</div>
<div class="form-group<?php echo form_error('form[password]') ? ' has-error' : ''; ?>">
<label class="col-sm-1 control-label"><?php echo $this->lang->line('password'); ?>:</label>
<div class="col-sm-4">
<input name="form[password]" type="password" maxlength="30" class="form-control"/>
<span class="help-block"><?php echo form_error('form[password]'); ?></span>
</div>
</div>
<div class="form-actions col-sm-offset-1">
<button type="submit" class="btn btn-danger col-sm-3"><i class="glyphicon glyphicon-user"></i> <?php echo $this->lang->line('login'); ?></button>
</div>
</form>
</div>
</div>
is it true that php7.1 is not supported by CI 3.0.0 so the error happened by this?

Trying to fetch image from database in codeigniter

I was uploaded Image in database Successfully. But not succeed to fetch image from database.
i want to fetch the image from database.
here is my code,
controller -> login.php
public function user_update(){
$this->load->model('login_model');
$this->form_validation->set_rules('fname', 'First Name','required');
$this->form_validation->set_rules('lname', 'Last Name','required');
$this->form_validation->set_rules('cnumber', 'Contact Number','required|min_length[10]|max_length[10]|numeric');
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'doc|docx|pdf|gif|jpg|png|xlsx';
$config['max_size'] = 10000;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
if (( ! $this->upload->do_upload('image')) && ($this->form_validation->run() === FALSE ))
{
$this->load->view('student/home');
}
else
{
$data = array('upload_data' => $this->upload->data());
$image_name=($data['upload_data']['file_name']);
$resume=base_url().$image_name;
$udata = array(
'id' => $this->input->post('id'),
'fname' => $this->input->post('fname'),
'mname' => $this->input->post('mname'),
'lname' => $this->input->post('lname'),
'email' => $this->input->post('email'),
'address' => $this->input->post('address'),
'cnumber' => $this->input->post('cnumber'),
'image'=> $resume
);
$this->login_model->Updateuser($udata);
}
}
model file:- login_model.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class login_model extends CI_Model
{
function Updateuser($param) {
$id = $param['id'];
$usar1 = array(
'Student_Name' => $param['fname'],
'm_name' => $param['mname'],
'l_name' => $param['lname'],
'Student_Email' => $param['email'],
'contact_no' => $param['cnumber'],
'Address' => $param['address'],
'image' => $param['image']
);
$this->db->where('Student_id', $id);
$this->db->update('students', $usar1);
//echo ($this->db->last_query());
//exit();
$sturesult = $this->login_model->get_student_list();
$data['Stulist'] = $sturesult;
$this->load->view('student/default_list',$data);
}
}
view file:- edit_info.php
<div class="container" id='main-layout' style="border-top: 1px solid #D14B54; background: #f5f5f5f5">
<div class="row">
<div class="col-md-8 col-sm-8">
<div class="ajaxResponse"><input type="hidden" name="ajaxResponse"></div>
<div class="row" style="padding: 10px 5px;">
<?php $id = $this->uri->segment(3); if(!empty($id)): ?>
<div class="">
<?php echo validation_errors();
echo $lname;
?>
<div class="thumbnail familycol" style="padding:16px">
<?php echo form_open_multipart('login/user_update'); ?>
<legend>Personal Information</legend>
<div class="row">
<?php //echo form_label('Id :'); ?> <?php echo form_error('id'); ?>
<input type="hidden" name="id" value="<?php echo $row->Student_id; ?>" class="form-control input-sm" placeholder="id"/>
<!-- <label class="required">First name</label>-->
<div class="col-xs-6 col-md-6">
<label for="First Name">First Name:</label>
<input type="input" name="fname" class="form-control" value="<?php echo $row->Student_Name;?>"/><font color="red"><?php echo form_error('fname'); ?></font>
</div>
<div class="col-xs-6 col-md-6">
<!--<label class="required">Middle name</label>-->
<label for="Last Name">Middle Name:</label>
<input type="input" name="mname" class="form-control" value="<?php echo $row->m_name?>"/><font color="red"><?php echo form_error('mname'); ?></font>
</div>
</div>
<div class="row">
<div class="col-xs-6 col-md-6">
<!--<label class="required">Last name</label>-->
<label for="Last Name">Last Name:</label>
<input type="input" name="lname" class="form-control" value="<?php echo $row->l_name ;?>"/><font color="red"><?php echo form_error('lname'); ?></font>
</div>
<div class="col-xs-6 col-md-6">
<?php echo form_label('Email :'); ?> <?php echo form_error('email'); ?>
<input type="text" name="email" value="<?php echo $row->Student_Email; ?>" class="form-control input-sm" placeholder="Email" />
</div>
</div>
<br>
<div class="row">
<div class="col-sm-6">
<!--<label>Address</label>-->
<label for="text">Address</label>
<textarea name="address" class="form-control"><?php echo $row->Address ;?></textarea><font color="red"><?php echo form_error('address'); ?></font>
</div>
<div class="col-sm-6">
<label for="text">Contact Number</label>
<textarea name="cnumber" class="form-control"><?php echo $row->contact_no ;?></textarea><font color="red"><?php echo form_error('cnumber'); ?></font>
</div>
<br/>
</div>
<br/>
<label for='uploaded_file'>Select A File To Upload:</label>
<input type="file" name="image" accept="image/*" value="upload">
<br/>
<button class="btn btn-lg btn-primary btn-block signup-btn" type="submit" style="margin-top: 5px;">
Update my Profile</button>
</div>
</div>
<?php endif;?>
</div>
</div>
</div>
</div>
Ok Lets Put Code in your View file:
<img src="<?php echo base_url('uploads/').$Stulist[$i]->image; ?>"
You have updated the wrong image directory of uploaded images in database.
Simply update the following lines of code
$data = array('upload_data' => $this->upload->data());
$image_name=($data['upload_data']['file_name']);
$resume=base_url().$image_name;
to
$data = array('upload_data' => $this->upload->data());
$image_name=($data['upload_data']['file_name']);
$resume=base_url('uploads').$image_name;
here the base_url points to the uploads directory where you have uploaded your images.

updating data in codiegniter with out selecting the file field in form

I am updating the data in database where I have images file too .So what i want is to put the condition on the image input field if the user want to update data as well as the image so the image will be updated accordingly and if user don't want to update image the rest of the data will be updated now I am having problem in getting the image input field data in controller. I am unable to get the filename directly in controller please help me out.
Here is the code.
function save_update()
{
$id=$this->input->post('id');
if(!empty($_FILES['userfile']['name']))
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '2048';
$config['max_width'] = '2000';
$config['max_height'] = '2000';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_successful', $error);
}
else
{
$data1 = $this->upload->data();
$filename=$data1['file_name'];
$data=array(
'pname'=>$this->input->post('product_name'),
'pprice'=>$this->input->post('product_price'),
'pquantity'=>$this->input->post('product_quantity'),
'pcategory'=>$this->input->post('category'),
'product_pic'=>$filename
);
$result=$this->cartmodel->update_data($data,$id,'product');
}
}
else
{
$data=array(
'pname'=>$this->input->post('product_name'),
'pprice'=>$this->input->post('product_price'),
'pquantity'=>$this->input->post('product_quantity'),
'pcategory'=>$this->input->post('category'),
'product_pic'=>$this->input->post('oldfile')
);
$result=$this->cartmodel->update_data($data,$id,'product');
if($result==true)
{
redirect('cart/admin');
}
else
{
echo '<script type="text/javascript">alert("sorry Could\'nt delete the file")</script>';
}
}
}
Html is here.
<form class="form-horizontal" method="post" action="<?php echo base_url(); ?>cart/save_update" enctype="multipart/form-data">
<?php
if(isset($specific))
{?>
<fieldset>
<legend>Form Components</legend>
<div class="control-group">
<label class="control-label" for="project_tittle">Product Name</label>
<div class="controls">
<input type="text" class="span6" id="typeahead" name="product_name" value=
"<?=$specific->pname; ?>">
</div>
</div>
<input type="hidden" name="id" value="<?=$specific->pid ?>">
<div class="control-group">
<label class="control-label" for="project_name">Price </label>
<div class="controls">
<input type="text" class="span6" id="typeahead" name="product_price"
value=
"<?=$specific->pprice; ?>">
</div>
</div>
<div class="control-group">
<label class="control-label" for="project_caption">Quantity </label>
<div class="controls">
<input type="number" class="span6" id="typeahead" name="product_quantity"
value=
"<?=$specific->pquantity; ?>">
</div>
</div>
<div class="control-group">
<label class="control-label" for="project_link">Category </label>
<div class="controls">
<select name="category">
<option value=
"<?=$specific->pcategory; ?>"><?=$specific->pcategory; ?></option>
</select>
</div>
</div>
<div class="control-group">
<label class="control-label" for="userfile">Product Pic </label>
<div class="controls">
<input type="file" class="span6" id="typeahead" name="userfile" >
</div>
</div>
<input type="hidden" name="oldfile" value="<?=$specific->product_pic; ?>" >
<div class="form-actions">
<button type="submit" class="btn btn-primary">Save changes</button>
<button type="reset" class="btn">Cancel</button>
</div>
</fieldset>
<?php
}
?>
</form>
Replace
if(!empty($_FILES['userfile']['name']))
With
if(isset($_FILES['userfile']))
It is best practice to use isset() to see whether the data is being set or not
Replace
if ( ! $this->upload->do_upload())
With
if ( ! $this->upload->do_upload($_FILES['userfile']['name']))

Resources