The right way of uploading multiple files using codeigniter - codeigniter

I am trying to upload multiple images at once but this code contain error and a error message is showing that Undefined index: photo1A. So help me to fix it.
function add_student_database(){
//echo '<pre>';print_r($_POST);print_r($_FILES);exit;
$this->load->library('upload');
if($this->input->post('stu_class')=='first' && $this->input->post('section')=='A')
{
$data = $this->input->post('student1A');
$img_data = $_FILES['photo1A'];
// echo '<pre>';print_r($_FILES);exit;
$count = count($_FILES['photo1A']);
$config['upload_path'] = './upload/';
$config['allowed_types'] = 'jpg|png|gif';
$config['max_size'] = '100000';
$config['max_width'] = '2000';
$config['max_height'] = '2000';
$this->load->library('upload', $config);
for($i=0;$i<$count;$i++)
{
$_FILES['photo1A']['name'] = $img_data['photo1A']['name'][$i];
$_FILES['photo1A']['type'] = $img_data['photo1A']['type'][$i];
$_FILES['photo1A']['tmp_name'] = $img_data['photo1A']['tmp_name'][$i];
$_FILES['photo1A']['error'] = $img_data['photo1A']['error'][$i];
$_FILES['photo1A']['size'] = $img_data['photo1A']['size'][$i];
$this->upload->initialize($config);
$this->upload->do_upload('photo1A');
}
$section = "A";
$stu_class = "class1";
}
elseif($this->input->post('stu_class')=='first' && $this->input->post('section')=='B')
{
$data = $this->input->post('student1B');
$section = "B";
$stu_class = "class1";
}
elseif($this->input->post('stu_class')=='second' && $this->input->post('section')=='A')
{
$data = $this->input->post('student2A');
$section = "A";
$stu_class = "class2";
}
elseif($this->input->post('stu_class')=='second' && $this->input->post('section')=='B')
{
$data = $this->input->post('student2B');
$section = "B";
$stu_class = "class2";
}
$this->marksheet_data->get_data($data, $section, $stu_class);
}

$message="";
$error=0;
$data1 = $this->input->post('data1');
$data2 = $this->input->post('data2');
$data3 = $this->input->post('data3');
$data.. = $this->input->post('data..');
$datan = $this->input->post('datan');
if($error==0)
{
$newdata = array(
'colName1_of_table' => $data1,
'colName2_of_table' => $data1,
'colName..._of_table'=>$data..,
'colNamen_of_table'=>$datan
);
$insert = $this->db->insert('tblName', $newdata);
$lastid=$this->db->insert_id();
if($insert)
{
//.............image upload.......................................//
$config['upload_path'] = PHYSICAL_PATH.'design/front/';
//$config['allowed_types'] = 'gif|png|jpeg|jpg';
$config['allowed_types'] = '*'; //allow all type of file with any extension
$config['max_size'] = 1024 * 8;
$config['encrypt_name'] = TRUE;
$this->load->library('upload', $config);
$status = "";
$msg = "";
$userpicture='';
if(!empty($_FILES["inputTAGnameOFfirstImage"]["name"]))
{
$file_element_name = 'inputTAGnameOFfirstImage';
if (!$this->upload->do_upload($file_element_name))
{
$status = 'error';
$msg = $this->upload->display_errors('', '');
}
else
{
$data = $this->upload->data();
if($data)
{
$config = array(
'source_image' => $data['full_path'],
'new_image' => $config['upload_path'] . '/thumbnail/',
'maintain_ratio' => true,
'width' => 100,
'height' => 100
);
$this->load->library('image_lib', $config);
$this->image_lib->resize();
$this->image_lib->clear(); /*this is important*/
$userpicture = $data['file_name'];
}
}
$image_data=array(
'colNameofImage_to_store' => $userpicture
);
$this->db->where('id', $lastid);
$insert_image = $this->db->update('tblName',$image_data);
}
//for Next image
$config1['upload_path'] = PHYSICAL_PATH.'design/back/';
//$config1['allowed_types'] = 'gif|png|jpeg|jpg';
$config1['allowed_types'] = '*';
$config1['max_size'] = 1024 * 8;
$config1['encrypt_name'] = TRUE;
$this->load->library('upload', $config1);
$this->upload->initialize($config1);
$status = "";
$msg = "";
$userpicture1='';
if(!empty($_FILES["inputTAGnameOFsecondImage"]["name"]))
{
$file_element_name = 'inputTAGnameOFsecondImage';
if (!$this->upload->do_upload($file_element_name))
{
$status = 'error';
echo $msg = $this->upload->display_errors('', '');
}
else
{
$data1 = $this->upload->data();
if($data1)
{
$config2 = array(
'source_image' => $data1['full_path'],
'new_image' => $config1['upload_path'] . '/thumbnail/',
'maintain_ratio' => true,
'width' => 100,
'height' => 100
);
// Initialize
$this->load->library('image_lib', $config2, 'image_lib_thumb');
//$this->image_lib->resize();
$this->image_lib_thumb->resize();
$this->image_lib_thumb->clear(); /*this is important*/
$userpicture1 = $data1['file_name'];
}
}
$new_image_data=array(
'second Image _colName_inTbl' => $userpicture1
);
$this->db->where('id', $lastid);
$insert_image = $this->db->update('tblName',$new_image_data);
}
}
return $insert;
}
Hope this help you. Question are welcome. Loop according to your need as you were trying to do.

Related

Codeigniter news image resize

How to resize the news picture? I never could.
My problem [source_image]
I use Codeigniter 3.1.6
Controller
public function insert_news() {
$config['upload_path'] = 'uploads/news/';
$config['allowed_types'] = 'gif|jpg|png';
$config['encrypt_name'] = TRUE;
$this->load->library('upload', $config);
if ($this->upload->do_upload('news_image'))
{
$image = $this->upload->data();
$image_url = $image['file_name'];
$db_insert ='upload/news/'.$image_url.'';
$data=array(
'news_title' => $this->input->post('news_title'),
'news_content' => $this->input->post('news_content'),
'news_image' => $db_insert,
'news_sef'=>sef($this->input->post('news_title'))
);
$this->load->model('vt');
$result = $this->vt->insert_news($data);
if ($result) {
echo "yes";
} else {
echo "no";
}
}
}
You can try this solution for your problem :
Controller :
<?php
public function insert_news() {
$config['upload_path'] = 'uploads/news/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '20240000245';
$config['overwrite'] = TRUE;
$config['encrypt_name'] = TRUE;
// You can change width & height
$imgage_width= 60;
$imgage_height= 60;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (!$this->upload->do_upload($field)) {
$error = $this->upload->display_errors();
// uploading failed. $error will holds the errors.
$this->form_validation->set_message('error',$error);
return FALSE;
} else {
$fdata = $this->upload->data();
$configer = array(
'image_library' => 'gd2',
'source_image' => $fdata['full_path'],
'maintain_ratio' => TRUE,
'width' => $imgage_width,
'height' => $imgage_height,
);
// Load Image Lib Library
$this->load->library('image_lib');
$this->image_lib->clear();
$this->image_lib->initialize($configer);
$this->image_lib->resize();
$img_data ['path'] = $config['upload_path'] . $fdata['file_name'];
// uploading successfull, now do your further actions
$data=array(
'news_title' => $this->input->post('news_title'),
'news_content' => $this->input->post('news_content'),
'news_image' => $img_data ['path'],
'news_sef'=>sef($this->input->post('news_title'))
);
$this->load->model('vt');
$result = $this->vt->insert_news($data);
if ($result) {
$this->form_validation->set_message('success',"News has been added successfully.");
redirect('contorller/action_name');
} else {
$this->form_validation->set_message('error',"Error in aading news");
redirect('contorller/action_name');
}
}
}
?>
I Hope it will Help you.

Image resize in codeigniter

I want to upload multiple images using two file input as well as resize them.
1. original image
2. 100 x 100
3. 400 x 200
All works fine but image lib not resize properly.It uploads images in original size.I have check following things.
1. Image lib load only once. which i load in __construct().
2. before initializing clear the image lib. $this->image_lib->clear();
function do_upload($name,$shop_code) {
$files = $_FILES;
if ($name == 'pre_photo_array') {
$photo_name = 'pre_photo_array';
} else {
$photo_name = 'post_photo_array';
}
$cpt = count($_FILES[$name]['name']);
--$cpt;
for ($i = 0; $i < $cpt; $i++) {
$_FILES[$photo_name]['name'] = $files[$photo_name]['name'][$i];
$_FILES[$photo_name]['type'] = $files[$photo_name]['type'][$i];
$_FILES[$photo_name]['tmp_name'] = $files[$photo_name]['tmp_name'][$i];
$_FILES[$photo_name]['error'] = $files[$photo_name]['error'][$i];
$_FILES[$photo_name]['size'] = $files[$photo_name]['size'][$i];
if ($photo_name == 'pre_photo_array') {
$config['upload_path'] = './assets/images/shop_images/pre_pic/';
} else {
$config['upload_path'] = './assets/images/shop_images/post_pic/';
}
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['overwrite'] = FALSE;
$config['file_name'] = $shop_code.'_'.time();
$this->upload->initialize($config);
if (!$this->upload->do_upload($photo_name)) {
$error = array('error' => $this->upload->display_errors());
$file_path[] = ' ';
} else {
$upload_data = $this->upload->data();
if ($name == 'pre_photo_array') {
$imgdata=exif_read_data('assets/images/shop_images/pre_pic/'.$upload_data['file_name'], 'IFD0');
$source_image_path = getimagesize('assets/images/shop_images/pre_pic/'.$upload_data['file_name']);
$source_image = 'assets/images/shop_images/pre_pic/'.$upload_data['file_name'];
$Pre_Thumbnail_new_image = 'assets/images/shop_images/pre_pic/Thumbnail/'.$upload_data['file_name'];
$Pre_ppt_new_image = 'assets/images/shop_images/pre_pic/PPT/'.$upload_data['file_name'];
} else {
$imgdata=exif_read_data('assets/images/shop_images/post_pic/'.$upload_data['file_name'], 'IFD0');
$source_image_path = getimagesize('assets/images/shop_images/post_pic/'.$upload_data['file_name']);
$source_image = 'assets/images/shop_images/post_pic/'.$upload_data['file_name'];
$Post_Thumbnail_new_image = 'assets/images/shop_images/post_pic/Thumbnail/'.$upload_data['file_name'];
$Post_ppt_new_image = 'assets/images/shop_images/post_pic/PPT/'.$upload_data['file_name'];
}
$rotation_angle = 0;
switch(isset($imgdata['Orientation'])) {
case 3:
$rotation_angle='180';
break;
case 6:
$rotation_angle='90';
break;
case 8:
$rotation_angle='90';
break;
}
if($name == 'pre_photo_array') {
$Thumbnail_new_image = $Pre_Thumbnail_new_image;
$ppt_new_image = $Pre_ppt_new_image;
} else {
$Thumbnail_new_image = $Post_Thumbnail_new_image;
$ppt_new_image = $Post_ppt_new_image;
}
// Create Thumbnail
$Thumbnail['image_library'] = 'gd2';
$Thumbnail['source_image'] = $source_image;
$Thumbnail['new_image'] = $Thumbnail_new_image;
$Thumbnail['create_thumb'] = TRUE;
$Thumbnail['thumb_marker'] = FALSE;
$Thumbnail['maintain_ratio'] = TRUE;
$Thumbnail['width'] = 100;
$Thumbnail['height'] = 100;
$Thumbnail['rotation_angle'] = $rotation_angle;
$this->image_lib->clear();
$this->image_lib->initialize($Thumbnail);
// resize image
$this->image_lib->resize();
$this->image_lib->rotate();
// handle if there is any problem
if(!$this->image_lib->resize()) {
echo "if";
echo $this->image_lib->display_errors();exit;
}
if($source_image_path[0] > $source_image_path[1]){
$width = 400;
$height = 200;
}else{
$width = 200;
$height = 400;
}
// Create PPt image
$ppt['image_library'] = 'gd2';
$ppt['source_image'] = $source_image;
$ppt['new_image'] = $ppt_new_image;
$ppt['create_thumb'] = FALSE;
$ppt['thumb_marker'] = FALSE;
$ppt['maintain_ratio'] = TRUE;
$ppt['width'] = 400;
$ppt['height'] = 200;
$ppt['rotation_angle']=$rotation_angle;
$this->image_lib->clear();
$this->image_lib->initialize($ppt);
// resize image
$this->image_lib->resize();
$this->image_lib->rotate();
// handle if there is any problem
if(!$this->image_lib->resize()) {
echo $this->image_lib->display_errors();exit;
}
if($photo_name == 'pre_photo_array') {
$path = 'assets/images/shop_images/pre_pic/';
} else {
$path = 'assets/images/shop_images/post_pic/';
}
$file_path[] = $path.$upload_data['file_name'];
}
}
return $file_path;
}
}
I am adding my Upload Image and Create Multiple Thumbnail Sizes in CodeIgniter Example. Hope it helps you.
//initialize the path where you want to save your images
function __construct(){
parent::__construct();
//return the full path of the directory
//make sure these directories have read and write permessions
$this->original_path = realpath(APPPATH.'../uploads/original');
$this->resized_path = realpath(APPPATH.'../uploads/resized');
$this->thumbs_path = realpath(APPPATH.'../uploads/thumbs');
}
function do_upload(){
$this->load->library('image_lib');
$config = array(
'allowed_types' => 'jpg|jpeg|gif|png', //only accept these file types
'max_size' => 2048, //2MB max
'upload_path' => $this->original_path //upload directory
);
$this->load->library('upload', $config);
$image_data = $this->upload->data(); //upload the image
//your desired config for the resize() function
$config = array(
'source_image' => $image_data['full_path'], //path to the uploaded image
'new_image' => $this->resized_path, //path to
'maintain_ratio' => true,
'width' => 128,
'height' => 128
);
//this is the magic line that enables you generate multiple thumbnails
//you have to call the initialize() function each time you call the resize()
//otherwise it will not work and only generate one thumbnail
$this->image_lib->initialize($config);
$this->image_lib->resize();
$this->image_lib->clear();
$config = array(
'source_image' => $image_data['full_path'],
'new_image' => $this->thumbs_path,
'maintain_ratio' => true,
'width' => 36,
'height' => 36
);
//here is the second thumbnail, notice the call for the initialize() function again
$this->image_lib->initialize($config);
$this->image_lib->resize();
$this->image_lib->clear();
}

codeigniter success insert to database but failed to insert uploaded file

I'm new here, so nice to meet you all.
I have trouble with codeigniter.
Maybe hard to tell so I will show all of my code :
my controller :
function upload_barang(){
$this->admin_model->login();
$this->admin_model->valid_product();
$config['upload_path'] = './produk/';
$config['allowed_types'] = 'jpg';
$config['max_size'] = '100';
$config['max_width'] = '400';
$config['max_height'] = '400';
$this->load->library('upload',$config);
$data['query'] = $this->db->get('kategori');
if($this->form_validation->run() == FALSE && ! $this->upload->do_upload()) {
$data['error'] = $this->upload->display_errors();
$data['success'] = '';
$this->load->view('backoffice/tambahbarang',$data);
} else {
$upload_data = $this->upload->data();
$data['error'] = '';
$data = array(
'idkategori' => $this->input->post('idkategori'),
'namabarang' => $this->input->post('nbarang'),
'image' => $upload_data['file_name'],
'status' => 1
);
$data_insert = $this->db->insert('barang',$data);
if($data_insert == TRUE) {
$data['query'] = $this->db->get('kategori');
$query = $this->db->get('barang');
$rows = $query->row();
$data['idbarang'] = $rows->idbarang;
$harga = array(
'idbarang' => $data['idbarang'],
'satuan' => $this->input->post('stcbarang'),
'harga' => $this->input->post('hbarang')
);
$this->db->insert('hargabarang',$harga);
$data['success'] = '<b>Barang Telah Ditambahkan</b>';
$this->load->view('backoffice/tambahbarang',$data);
} else {
$data['success'] = '<b>Barang Gagal Ditambahkan</b>';
$this->load->view('backoffice/tambahbarang',$data);
}
}
}
This code success insert all of data to database, except on column 'image' that I fill with '$upload_data['file_name']' this empty on my database.
what's wrong with my code?
please help thanks
You can use this format:
/*
* ------- Start Image Upload---------
*/
$config['upload_path'] = 'image/product_images/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '400';
$config['max_height'] = '400';
$this->load->library('upload', $config);
$this->upload->initialize($config);
$error='';
$fdata=array();
if ( ! $this->upload->do_upload('product_image'))
{
$error = $this->upload->display_errors();
echo $error;
exit();
}
else
{
$fdata = $this->upload->data();
$data['product_image']=$config['upload_path'] .$fdata['file_name'];
}
/*
* --------End Image Upload---------
*/
$this->super_admin_model->save_product_info($data);

csv file is uploaded locally but not online in codeigniter

my csv file is locally uploading perfectly but when i upload it on my website then its not uploading following is my upload function
function upload_title()
{
if(isset($_POST['upload_titles'])) // check if submit button is clicked
{
$config['upload_path'] = 'uploads/title/';
$config['allowed_types'] = 'csv';
$config['max_size'] = '5000';
$config['overwrite'] = TRUE;
$config['encrypt_name'] = TRUE;
$config['remove_spaces'] = TRUE;
$this->load->library('upload', $config);
$this->upload->initialize($config);
$this->upload->do_upload();
$temp = $this->upload->data('userfile');
$article_titlelist = $temp['file_name'];
if($this->upload->do_upload())
{
$this->load->library('csvreader');
$result = $this->csvreader->parse_file('./uploads/title/'.$article_titlelist);
foreach($result as $val)
{
$articletitle = $val['title'];
$title_category = $val['category'];
$mDate = date('Y-m-d H:i:s');
$data = array(
'article_title' => $articletitle,
'title_category' => $title_category,
'article_cdate' => $mDate,
'article_mdate' => $mDate
);
$this->article_model->addtitle($data);
}
redirect('admin/article/title_listing');
}
}
$error = array('error' => $this->upload->display_errors());
$data['page_title'] = 'Article Title';
$data['content'] = $this->load->view('admin/add_title', $error, true);
$this->load->view('admin/template', $data);
}
i have check it online its not going in do_upload condition

How to update image data with form_upload in codeigniter

How to edit this data with image inside.Data have been updated when i press the button "submit", but the old image can not change with the new image
if($this->input->post('go_upload'))
{
validation.......
$IdPerumahan = $this->uri->segment(3);
if ($this->form_validation->run() == TRUE)
{
$lokasi_file = $_FILES['userfile']['tmp_name'];
$nama_file = $_FILES['userfile']['name'];
$ukuran_file = $_FILES['userfile']['size'];
$direktori_file = "./images/$nama_file";
if (empty($lokasi_file))
{
$this->load->model('PerumahanMDL');
$this->PerumahanMDL->SimpanUbahPerumahan($IdPerumahan);
redirect('PerumahanCON');
} else
{
$config['upload_path'] = './images/';
$config['allowed_types'] = 'gif|jpg|png|bmp|jpeg|png';
$config['max_size'] = '2000';
$config['overwrite'] = 'true';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
$this->upload->initialize($config);
$field_name = "userfile";
if(!$this->upload->do_upload("userfile"))
{
$data['data']= 'TambahPerumahanVW';
$this->load->view('Desain2',$data);
}else
{
$asdf = $this->upload->data();
$config = array(
'source_image' => $asdf['full_path'],
'new_image' => './images/thumb',
'maintain_ration' => true,
'width' => 180,
'height' => 120
);
$this->load->library('image_lib', $config);
$this->image_lib->resize();
$userfile = $_FILES['userfile']['name'];
$this->load->model('PerumahanMDL');
$this->PerumahanMDL->UbahDataPerumahan($userfile,$IdPerumahan);
redirect('PerumahanCON');
}
}
}
}
}
Add overwrite = true.
$this->load->library('upload', $config);
$this->upload->overwrite = true;

Resources