Undefine Variable id_gambar codeigniter - codeigniter

if I try to enter data, it can enter the database, but it doesn't display
// GAMBAR
public function gambar($id_program)
{
// panggil model
$program = $this->program_model->detail($id_program);
$gambar = $this->program_model->gambar($id_program);
// validasi input
$valid = $this->form_validation;
$valid->set_rules('judul_gambar', 'Judul/Nama Gambar', 'required',
array( 'required' => '%s harus diisi'));
if($valid->run()) {
$config['upload_path'] = './assets/upload/image/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '2400'; // KiloByte
$config['max_width'] = '3000';
$config['max_height'] = '3000';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('gambar')) {
$data = array( 'title' => 'Tambah Gambar Program: '.$program->nama_program,
'program' => $program,
'gambar' => $gambar,
'error' => $this->upload->display_errors(),
'isi' => 'admin/program/gambar'
);
$this->load->view('admin/layout/wrapper', $data, FALSE);
}else {
$upload_data = array('uploads' =>$this->upload->data());
// Image Editor
$config['image_library'] = 'gd2';
$config['source_image'] = './assets/upload/image/'.$upload_data['uploads']['file_name'];
$config['new_image'] = './assets/upload/image/thumbs/';
$config['create_thumb'] = TRUE;
$config['quality'] = "100%";
$config['maintain_ratio'] = TRUE;
$config['width'] = 360; // Pixel
$config['height'] = 360; // Pixel
$config['thumb_marker'] = '';
$this->load->library('image_lib', $config);
$this->image_lib->resize();
// end create gambar
$inp = $this->input;
$data = array( 'id_gambar' => $id_gambar,
'judul_gambar' => $inp->post('judul_gambar'),
'gambar' => $upload_data['uploads']['file_name'],
);
$this->program_model->tambah_gambar($data);
$this->session->set_flashdata('sukses', 'Data gambar telah ditambah');
redirect(base_url('admin/program/gambar/'.$id_program),'refresh');
}}
$data = array( 'title' => 'Tambah Gambar Program: '.$program->nama_program,
'program' => $program,
'gambar' => $gambar,
'isi' => 'admin/program/gambar'
);
$this->load->view('admin/layout/wrapper', $data, FALSE);
}

Is this working for you ?
'id_gambar' => $gambar["id_gambar"];
$id_gambar is not defined.

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.

How to use blob using codeigniter?

I'm currently creating a program using codeigniter where I want to have image upload and save it as blob to database. The problem is when I tried to upload the image, I received this error
The path to the image is not correct.
Your server does not support the GD function required to process this type of image.
Here's my code in controller:
public function do_upload() {
if($this->session->userdata('logged_in')) {
//print_r($_FILES);
$config = array(
'upload_path' => './public/img/uploads',
'upload_url' => base_url().'public/img/uploads',
'allowed_types' => 'gif|jpg|png|jpeg',
//'overwrite' => TRUE,
'max_size' => '1000KB',
'max_width' => '1024',
'max_height' => '768',
//'encrypt_name' => true,
);
$this->load->library('upload', $config);
//for($i=0; $i<count($_FILES['userfile']['name']); $i++) {
if (!$this->upload->do_upload()) {
$error = array('error' => $this->upload->display_errors());
$this->load->view('v_page/header_view');
$this->load->view('v_document/upload_error', $error);
$data['folderName'] = $this->dropdown->get_folder_details();
$data['departmentName'] = $this->dropdown->get_department_details();
$this->load->view('v_document/upload_view', $data);
$this->load->view('v_page/footer_view');
}
else {
$upload_data = $this->upload->data();
$this->_createThumbnail($upload_data['file_name']);
$data['thumbnail_name'] = $upload_data['raw_name']. '_thumb' .$upload_data['file_ext'];
$file_array = array(
'image' => "",
'image_name' => $upload_data['file_name'],
//'description' => "",
'date_created' => date('Y-m-d H:i:s', now()),
'date_modified' => date('Y-m-d H:i:s', now()),
'size' => $upload_data['file_size'],
'type' => $upload_data['image_type'],
'width' => $upload_data['image_width'],
'height' => $upload_data['image_height'],
);
$this->load->database();
$this->db->insert('tbl_image', $file_array);
$data = array('upload_data' => $this->upload->data());
$this->load->view('v_document/upload_success', $data);
}
//}
}
else {
redirect('login', 'refresh');
}
}
public function _createThumbnail($filename) {
$config['image_library'] = "gd2";
$config['source_image'] = "uploads/" .$filename;
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = "80";
$config['height'] = "80";
$this->load->library('image_lib',$config);
if(!$this->image_lib->resize()) {
echo $this->image_lib->display_errors();
}
}
The image is uploaded successfully but the blob didn't work.
Try below suggestion:
Change from:
$config['source_image'] = "uploads/" .$filename;
To:
$config['source_image'] = "./uploads/" .$filename;

The right way of uploading multiple files using 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.

How to delete a image file?

How can i delete image files from uploaded folder in codeigniter from delete button??
Can any one guide me ???
Here is my controller that upload image files
private function upload() {
$config['upload_path'] = 'assets/uploads/orginal/';
$config['allowed_types'] = 'gif|jpg|png|jpeg|x-png';
$config['max_size'] = '500';
$config['max_width'] = '1600';
$config['max_height'] = '1200';
$config['remove_spaces'] = TRUE;
$this->load->library('upload', $config);
if ($this->upload->do_upload()) {
$img = $this->upload->data();
// create thumbnail
$new_image = 'assets/uploads/thumbs/' . $img['file_name'];
$c_img_lib = array(
'image_library' => 'gd2',
'source_image' => $img['full_path'],
'maintain_ratio' => TRUE,
'width' => 100,
'height' => 100,
'new_image' => $new_image
);
$this->load->library('image_lib', $c_img_lib);
$this->image_lib->resize();
} else {
$this->data['error'] = $this->upload->display_errors();
}
}
Use the unlink() function:
unlink($new_image);
http://pt1.php.net/manual/en/function.unlink.php

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