codeigniter success insert to database but failed to insert uploaded file - codeigniter

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);

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.

Rename file when uploading

I would like to rename a file when I upload this file.
So the content of the add_land_short should get the name of the file.
So if the input test is, the file test.png is.
Controllers:
public function insert_land()
{
$land_short = $this->input->post('add_land_short');
$config['upload_path'] = './assets/images/site/flag/';
$config['allowed_types'] = '*';
$config['max_size'] = '2048';
$config['max_width'] = '2000';
$config['max_height'] = '2000';
$this->load->library('upload', $config);
if (!$this->upload->do_upload())
{
$errors = array('error' => $this->upload->display_errors());
$post_image = 'noimage.jpg';
}
else
{
$data = array('upload_data' => $this->upload->data());
$post_image = $_FILES['userfile']['name'];
}
$this->Admin_model->insert_land();
redirect('admin/land/');
}
Model:
public function insert_land()
{
$this->db->where('tb_land_name', $this->input->post('add_land_name'));
$this->db->where('tb_land_kurzel', $this->input->post('add_land_short'));
$this->db->where('tb_land_img', $this->input->post('add_land_short'));
$result = $this->db->get('db_land');
if($result->num_rows() < 1)
{
$data = array( 'tb_land_name' => $this->input->post('add_land_name'),
'tb_land_kurzel' => $this->input->post('add_land_short'),
'tb_land_last_buy' => date('Y-m-d'),
'tb_land_img' => $this->input->post('add_land_short'),
);
return $this->db->insert('db_land', $data);
}
}
Try This
$file_name = time() . rand(0, 9999) . "." . pathinfo($_FILES['userfile']['name'], PATHINFO_EXTENSION);
$land_short = $this->input->post('add_land_short');
$config['upload_path'] = FCPATH . 'assets/images/site/flag/';
$config['allowed_types'] = '*';
$config['max_size'] = '2048';
$config['max_width'] = '2000';
$config['max_height'] = '2000';
$config['file_name'] = $file_name;
$this->load->library('upload', $config);
if (!$this->upload->do_upload()) {
$errors = array('error' => $this->upload->display_errors());
$post_image = 'noimage.jpg';
redirect('admin/land/');
} else {
$data = array('upload_data' => $this->upload->data());
$post_image = $file_name;
}

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.

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