I'm trying to upload image in codeigniter, but I got problem in , $this->upload->do_upload('imgname'). if condition not execute, only elese execute and show error message is 'You did not selected file to upload'. but while remove the form upload in all code then it execute perfectly....
controller:
public function add_news()
{
$post = $this->input->post();
unset($post['submit']);
$this->load->model('Adminmodel','addNews');
if(!is_dir('uploads'))
{
mkdir(base_url().'uploads',0777,true);
}
if(!is_dir('uploads/news'))
{
mkdir('uploads/news',0777,true);
}
$config = [
'upload_path'=>'uploads/news',
'allowed_types'=>'png|jpg|jpeg|gif',
'encrypt_name'=>'0777',
];
$this->load->library('upload', $config);
if($this->upload->do_upload('newsimg'))
{
$data = $this->upload->data();
$file_path = base_url().'uploads/news/'.$data["raw_name"].$data["file_ext"];
$post['newsimg'] = $file_path;
//$this->revEdit->reg_info($id,$post);
$post['posted'] = date('d-m-Y');
$this->addNews->store_news($post);
$this->session->set_flashdata('newsAlert','Yeh! News Added Successfully.');
redirect('Admin/news');
}
else
{
$newsdata = $this->addNews->news_list();
$upload_error = $this->upload->display_errors();
$this->load->view('Admin/news',compact('upload_error','newsdata'));
}}
view:
<?=form_open('Admin/add_news',['class'=>'form'],['aid'=>$this->session->userdata('adminId')])?>
<div class="row">
<div class="col-sm-12">
<div class="form-group">
<label>News Heading</label>
<?=form_input(['name'=>'title','class'=>'form-control','placeholder'=>'Enter News Title','value'=>set_value('title')])?>
</div>
<div class="form-group">
<label>News Details</label>
<?=form_textarea(['name'=>'body','class'=>'form-control','placeholder'=>'Enter News Body','value'=>set_value('body')])?>
</div>
<div class="form-group col-sm-3">
<div class="fileUpload btn btn-primary" data-toggle="tooltip" data-placement="top" title="PDF file only">
<span><i class="fa fa-cloud-upload" aria-hidden="true"></i> Select File <span id="Percentage" class="percentage text-center"></span></span>
<?=form_upload(['name'=>'newsimg','class'=>'upload','id'=>'file1','onchange'=>'loadPdf(event)'])?>
</div>
</div>
<div class="col-sm-12">
<div id="myProgress" class="progress">
<div id="myBar" class="bar progress-bar" role='progressbar'>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<?=form_reset('reset','Reset',['class'=>'btn btn-md btn-primary'])?>
<?=form_submit('submit','Post',['class'=>'btn btn-md btn-success'])?>
</div>
<?=form_close()?>
model:
public function store_news($array)
{
return $this->db->insert('tbl_news', $array);
}
you are not using enctype='multipart/form-data'
try this
<?=form_open_multipart('Admin/add_news',['class'=>'form'],['aid'=>$this->session->userdata('adminId')])?>
You need to use form_open_multipart instead of form_open so that enctype can be added in form
Change
<?=form_open('Admin/add_news',['class'=>'form'],['aid'=>$this->session->userdata('adminId')])?>
to:
<?=form_open_multipart('Admin/add_news',['class'=>'form'],['aid'=>$this->session->userdata('adminId')])?>
You need to use multipart/form-data for file uploading.
So use form_open_multipart instead of form_open.
echo form_open_multipart('Admin/add_news');
function upload_add($name) { $this->load->helper('form'); $config['upload_path'] = 'upload/add_image/'; $config['allowed_types'] = 'gif|jpg|png'; $config['max_size'] = '30000'; $config['max_width'] = '102400'; $config['max_height'] = '76800'; $this->load->library('upload', $config); $this->upload->initialize($config); if (!$this->upload->do_upload($name)) { $data = array('msg' => $this->upload->display_errors()); } else { $data = array('msg' => "success"); $databasea['upload_data'] = $this->upload->data(); $this->load->library('image_lib'); return $databasea['upload_data']['file_name']; } return ''; }
Related
this is controller.php ie college_panel
public function college_logo($collg_id = '')
{
$this->data['active'] = 'manage_logo';
if ($this->session->userdata("user_login")) {
if ($this->input->post()) {
$config['upload_path'] = './uploads/college_logo';
$config['allowed_types'] = 'jpg|png|jpeg';
$data1['img_name'] = $_FILES['logo_img']['name'];
$this->load->library('upload', $config);
if (!$this->upload->do_upload('logo_img')) {
$upload_error = array('error' => $this->upload->display_errors());
} else {
echo "<script>alert('College Logo upload successfully...');</script>";
$this->session->set_flashdata('College Logo upload successfully', 'updated');
}
$data['collg_id'] = $this->input->post('collg_id');
$data['logo_img'] = $data1['img_name'];
$row = 0;
if ($row == 0) {
$result1 = $this->front->update_table('tbl_college', array('collg_id' => $collg_id), $data);
}
}
$result = $this->front->get_data_where('tbl_college', array('collg_id' => $collg_id));
$data['result'] = $result;
$data['email'] = $this->input->post('email');
$data['password'] = $this->input->post('password');
$data['isactive'] = 1;
$old_data = $this->front->get_data_where('tbl_login', array('isactive' => 1));
$data['old_data'] = $old_data;
$record = $this->front->get_data_where('tbl_college', array('collg_id' => $collg_id));
$data['record'] = $record[0];
$this->load->view('collg_admin/header', $data, $this->data);
$this->load->view('collg_admin/logo', $data);
} else {
redirect(base_url() . 'login');
}
}
this is view code ie logo.php
<div class="container">
<div class="panel panel-default upload">
<div class="panel-heading text-center">
<h2>College Logo</h2>
</div>
<br><br>
<div class="panel-body">
<div class="row">
<center>
<div class="col-md-12">
<?php if (isset($result[0]->logo_img) && !empty($result[0]->logo_img)) {?>
<img class=" img-circle" src="<?php echo base_url(); ?>uploads/college_logo/<?php echo $result[0]->logo_img; ?>" height="200" width="200"><br>
<?php } else {?>
<img style="" src="<?php echo base_url(); ?>assets/images/default_logo.png" height="200" width="600"><br>
<!-- <p>College Logo are not available.</p>-->
<?php }?>
</div>
</center>
<div class="col-md-offset-2 col-md-7">
<form action="<?php echo base_url(); ?>access/college_panel/college_logo/<?php echo $result[0]->collg_id; ?>" method="post" enctype="multipart/form-data" role="form">
<div class="form-group">
<!-- <?php //print_r($result);?>-->
<input type="hidden" name="collg_id" id="id_hh" value="<?php echo $result[0]->collg_id; ?>" autofocus="" class="form-control" style="width: 100px;">
<br><br>
<input class="form-control" type="file" accept="image/*" name="logo_img" value="<?php echo $result[0]->logo_img; ?> " required /><br>
<div class="col-md-offset-2 col-md-8 text-center">
<button class="btn btn-info" onclick="">Submit</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
I want to compress image on upload using codeigniter .when i m trying to upload more then 200 kb image size or mor then 200 then it hsould compress the image size on upload image in codeigniter.how to compress the image on upload of image. i just want to compress the image of the file using codeigniter if need more code will help you
I want to compress an image on upload using CodeIgniter.
When I'm trying to upload an image larger than 200 kb in size then it should compress the image size on upload image in CodeIgniter.
This is controller.php, ie college_panel:
public function college_logo($collg_id='')
{
$this->data['active']='manage_logo';
if($this->session->userdata("user_login")){
if($this->input->post()){
$config['upload_path']='./uploads/college_logo';
$config['allowed_types'] = 'jpg|png|jpeg';
$data1['img_name'] = $_FILES['logo_img']['name'];
$this->load->library('upload',$config);
if (!$this->upload->do_upload('logo_img'))
{
$upload_error=array('error'=>$this->upload->display_errors());
}
else
{
echo "<script>alert('College Logo upload successfully...');</script>";
$this->session->set_flashdata('College Logo upload successfully', 'updated');
}
$data['collg_id']=$this->input->post('collg_id');
$data['logo_img']=$data1['img_name'];
$row=0;
if($row==0)
{
$result1=$this->front->update_table('tbl_college',array('collg_id'=>$collg_id),$data);
}
}
$result=$this->front->get_data_where('tbl_college',array('collg_id'=>$collg_id));
$data['result']=$result;
$data['email']=$this->input->post('email');
$data['password']=$this->input->post('password');
$data['isactive']=1;
$old_data=$this->front->get_data_where('tbl_login',array('isactive'=>1));
$data['old_data']=$old_data;
$record=$this->front->get_data_where('tbl_college',array('collg_id'=>$collg_id));
$data['record']=$record[0];
$this->load->view('collg_admin/header',$data,$this->data);
$this->load->view('collg_admin/logo',$data);
}
else
{
redirect(base_url().'login');
}
}
This is view code ie logo.php:
<div class="container">
<div class="panel panel-default upload">
<div class="panel-heading text-center"><h2>College Logo</h2></div>
<br><br>
<div class="panel-body">
<div class="row">
<center>
<div class="col-md-12">
<?php if (isset($result[0]->logo_img) && !empty($result[0]->logo_img)) { ?>
<img class=" img-circle" src="<?php echo base_url();?>uploads/college_logo/<?php echo $result[0]->logo_img;?>" height="200" width="200"><br>
<?php } else {?>
<img style="" src="<?php echo base_url();?>assets/images/default_logo.png" height="200" width="600"><br>
<!-- <p>College Logo are not available.</p>-->
<?php } ?>
</div>
</center>
<div class="col-md-offset-2 col-md-7">
<form action="<?php echo base_url();?>access/college_panel/college_logo/<?php echo $result[0]->collg_id;?>" method="post" enctype="multipart/form-data" role="form">
<div class="form-group">
<!-- <?php //print_r($result);?>-->
<input type="hidden" name="collg_id" id="id_hh" value="<?php echo $result[0]->collg_id;?>" autofocus="" class="form-control" style="width: 100px;" >
<br><br>
<input class="form-control" type="file" accept="image/*" name="logo_img" value="<?php echo $result[0]->logo_img; ?> " required/><br>
<div class="col-md-offset-2 col-md-8 text-center">
<button class="btn btn-info" onclick="">Submit</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
How to compress the image on upload?
$config['upload_path'] = './upload/';
$config['allowed_types'] = 'jpg|jpeg|png|gif';
$this->load->library('upload', $config);
if(!$this->upload->do_upload('image_file'))
{
echo $this->upload->display_errors();
}
else
{
$data = $this->upload->data();
$config['image_library'] = 'gd2';
$config['source_image'] = './upload/'.$data["file_name"];
$config['create_thumb'] = FALSE;
$config['maintain_ratio'] = FALSE;
$config['quality'] = '60%';
$config['width'] = 200;
$config['height'] = 200;
$config['new_image'] = './upload/'.$data["file_name"];
$this->load->library('image_lib', $config);
$this->image_lib->resize();
$this->load->model('main_model');
$image_data = array(
'name' => $data["file_name"]
);
$this->main_model->insert_image($image_data);
echo $this->main_model->fetch_image();
//echo '<img src="'.base_url().'upload/'.$data["file_name"].'" width="300" height="225" class="img-thumbnail" />';
}
This is my code :
if (count($template) >=1) { ?>
<div class="mdl-cell mdl-cell--4-col" id="select">
<div class="mdl-card mdl-shadow--4dp">
<div class="mdl-card__title">
<h2 class="mdl-card__title-text">select <?php echo $module[$i]->im_name; ?> Template</h2>
</div>
<div class="mdl-card__media">
<?php echo $template[0]->iut_tempname; ?>
</div>
<a href="<?php echo base_url("Account/template_list/"); ?>" class="btn btn-info btn-lg">
<span class="glyphicon glyphicon-cog"></span>
</a>
</div>
</div>
<?php
}
I want to pass o/p of this im_name; ?> to controller .
Hope this will help you :
Create a folder uploads in your views folder and use VIEWPATH for the upload_path like this :
$config['upload_path'] = VIEWPATH.'uploads/';
Your code should be like this :
if (isset($_FILES["image_file"]["name"]))
{
$config['upload_path'] = VIEWPATH.'uploads/';
$config['allowed_types']= 'txt|php|html';
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (!$this->upload->do_upload('image_file'))
{
echo $this->upload->display_errors();
}
else
{
$data = $this->upload->data();
echo '<img src="'.base_url().'uploads/'.$data["file_name"].'" />';
}
}
For more : https://www.codeigniter.com/user_guide/general/reserved_names.html
i have created the root directory and set its path, i done the front end as was. the ide is not giving any error but when i proceed it say you did not select any image.
the controller code:
class Images extends MY_Controller
{
function __construct()
{
parent::__construct();
}
function index()
{
$this->data['displayname'] = $this->admin->lastname;
$this->data['images'] = Image::all();
$this->data['btn'] = 'Save';
$this->data['content'] = 'admin/images/index';
$this->load->view('layouts/admin', $this->data);
}
public function do_upload() {
$config['upload_path'] = './uploads/';
$config['allowed_types'] = '*';
//$config['max_size'] = 100;
//$config['max_width'] = 1024;
//$config['max_height'] = 768;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('images'))
{
$error = array('error' => $this->upload->display_errors());
// echo $this->upload->display_errors();
$this->load->view('index', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}
}
}
the view code:this is the form for choosing the file
<?= form_open('admin/images/do_upload') ?>
<label><?= form_upload('images[]')?></label>
<div class="input-group-btn">
<button class="btn btn-success add-image" type="button"><i class="glyphicon glyphicon-plus"></i> Add</button>
</div>
</div>
</ul>
<div class="line"></div>
<?= form_submit('', 'Submit', 'class="publish-btn"') ?>
<?= form_close()?>
<?= form_open_multipart('admin/images/do_upload') ?>
Use the above code instead of
<?= form_open('admin/images/do_upload') ?>
i added the multipart to my form which created an issue of invalid path which i solved by initializing the library after adding
$this->upload->initialize($config);
after this line of loading the library.
$this->load->library('upload', $config);
first load the library then initialize.
but still the problem of an selection is not solved
i tried a couple of other codes too.
well after a lot of research and trying lots of codes and i solved my issue
here's the full thing
the controller code:
public function create($id)
{
$config ['upload_path'] = 'uploads';
$config ['allowed_types'] = 'gif|jpg|png';
$config ['encrypt_name'] = TRUE;
$this->upload->initialize($config);
$package = Package::find($id);
$files = $_FILES;
$cpt = count($_FILES ['images'] ['name']);
for ($i = 0; $i < $cpt; $i++) {
$_FILES ['images'] ['name'] = $files ['images'] ['name'] [$i];
$_FILES ['images'] ['type'] = $files ['images'] ['type'] [$i];
$_FILES ['images'] ['tmp_name'] = $files ['images'] ['tmp_name'] [$i];
$_FILES ['images'] ['error'] = $files ['images'] ['error'] [$i];
$_FILES ['images'] ['size'] = $files ['images'] ['size'] [$i];
if ($this->upload->do_upload('images')){
$upload_data = $this->upload->data();
$file_name = $upload_data['file_name'];
$package->create_images(array('image_location' => $file_name));
}
}
redirect('admin/packages/');
}
}
the view code:
<div class="container">
<?= form_open_multipart('admin/images/create/' . $this->uri->segment(4)) ?>
<div class="row">
<div class="col-md-8 publish">
<h4>Image Gallery</h4>
<div class="line"></div>
<ul>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script>
<div class="input-group control-group after-add-image">
<label><input type="file" name="images[]"></label>
<div class="input-group-btn">
<button class="btn btn-success add-image" type="button"><i class="glyphicon glyphicon-plus"></i> Add</button>
</div>
</div>
</ul>
<div class="line"></div>
<?= form_submit('', 'Submit', 'class="publish-btn"') ?>
<?= form_close()?>
<div class="clear-all"></div>
<!-- Copy Fields-These are the fields which we get through jquery and then add after the above input,-->
<div class="copy-field" style="display: none">
<div class="control-group input-group" style="margin-top:10px">
<label><label><input type="file" name="images[]"></label></label>
<div class="input-group-btn">
<button class="btn btn-danger delete" type="button"><i class="glyphicon glyphicon-delete"></i> Remove</button>
</div>
</div>
</div>
<script type="text/javascript">
$(document).ready(function() {
//here first get the contents of the div with name class copy-fields and add it to after "after-add-more" div class.
$(".add-image").click(function(){
var html = $(".copy-field").html();
$(".after-add-image").after(html);
});
//here it will remove the current value of the remove button which has been pressed
$("body").on("click",".delete",function(){
$(this).parents(".control-group").remove();
});
});
</script>
its getting all the images dynamically, uploading them to the directory and then stores the path to the database
done!
the image for front end
In file uploading case you should use
<?php form_open_multipart('admin/images/do_upload') ?>
Not this
<?php form_open('admin/images/do_upload') ?>
Compress and image upload in to a database table without store image in upload folder using codeigniter. I am not getting any error, but images are not inserted in to a table. Any mistake? Please suggest me.
Controller
public function buyer_profile_image() {
$file_type = $_FILES['image']['type'];
$allowed = array("image/jpeg", "image/gif", "image/png");
if (!in_array($file_type, $allowed)) {
$this->load->view('buyerprofile');
} else {
$source_img = $_FILES['image']['tmp_name'];
$destination_img = 'destination .jpg';
$d = $this->compress($source_img, $destination_img, 50);
$image = addslashes($d);
$image = file_get_contents($image);
$image = base64_encode($image);
if ($image == "") {
$_SESSION["errmsg"] = "please select image to post";
$this->load->view('buyerprofile');
} else {
$data = array(
'profile_image' => $image,
);
$this->Profile_model->Product_insert($data);
$this->load->view('buyerprofile');
}
}
}
model
public function Product_insert($data){
$this->db->insert('siddu',$data);
view page
<form action="<?php echo base_url(); ?>Index.php/Profile_cntrl/buyer_profile_image" method="post" enctype="multipart/form-data">
<div class="text-center profile-bg">
<div class="user-bg"></div>
<a href="#" class="user-img">
<img src="<?php echo base_url(); ?>images/prasanthi.jpg" class="img-circle img-user" alt="" width="100px;" height="100px;"/>
</a>
<div class="user-info">
<span class="">xxxxxxx</span>
<div class="user-location">
<i class="fa fa-map-marker"></i> Bangalore
</div>
</div>
<div class="change-profile">
<!--<span data-role="upload" class="upload-trigger"><i class="fa fa-plus"></i> Change Profile</span>-->
<div class="attachment">
<input type="file" name="image" id="image"/>
</div>
<input type="submit" value="upload" class="btn btn-success" style="width:70px;">
</div>
</div>
</form>