How to update table with new image data using codeigniter - php

I have a form to upload image-path and other image-data. This works well when I choose to upload a new image.
How can I prevent that my image-path is set to blank if I submit my form without choosing a new image?
here is my controller:
function updatewaterpurifier($id)
{
$name=$this->input->post('name');
$this->load->model('user');
//$name = $this->input->post('name');
if($this->input->post('submit'))
{
//Check whether user upload picture
if(!empty($_FILES['picture']['name'])){
$new_file_name = $this->input->post('name');
$config['upload_path'] = 'uploads/images/';
$config['allowed_types'] = 'jpg|jpeg|png|gif|pdf';
$config['file_name'] = $new_file_name;
$config['max_size'] = 2048;
//Load upload library and initialize configuration
$this->load->library('upload',$config);
$this->upload->initialize($config);
if($this->upload->do_upload('picture')){
$uploadData = $this->upload->data();
$picture = $uploadData['file_name'];
}else{
$picture = '';
}
}else{
$picture = '';
}
//Prepare array of user data
$userData = array(
'product_brand'=> $this->input->post('brand'),
'product_name' => $this->input->post('name'),
'product_price' => $this->input->post('price'),
'product_techno' => $this->input->post('technology'),
'product_des' => $this->input->post('description'),
'product_image' => $picture
);
$this->db->where('id', $id);
$this->db->update('products', $userData);
//Pass user data to model
//Storing insertion status message.
}
//Form for adding user data
$this->load->model('user');
$this->data['h']= $this->user->editpurifier($id);
$this->load->view('editwaterpurifier', $this->data, FALSE);
}

if you are updating your table without previous image upload, your variable $picture=''. Use that to unset product_image from the array $userData():
$userData = array(
'product_brand'=> $this->input->post('brand'),
'product_name' => $this->input->post('name'),
'product_price' => $this->input->post('price'),
'product_techno' => $this->input->post('technology'),
'product_des' => $this->input->post('description'),
'product_image' => $picture
);
if ($picture==''){
unset($userData['product_image']);
}
This removes product_image from the array and won't updatinge the image path as blank.

In controller
function updatewaterpurifier($id)
{
$this->load->model('user');
if($this->input->post('submit'))
{
//Prepare array of user data
$userData = array(
'product_brand'=> $this->input->post('brand'),
'product_name' => $this->input->post('name'),
'product_price' => $this->input->post('price'),
'product_techno' => $this->input->post('technology'),
'product_des' => $this->input->post('description')
);
$name=$this->input->post('name');
//Check whether user upload picture
if(!empty($_FILES['picture']['name']))
{
$new_file_name = $this->input->post('name');
$config['upload_path'] = 'uploads/images/';
$config['allowed_types'] = 'jpg|jpeg|png|gif|pdf';
$config['file_name'] = $new_file_name;
$config['max_size'] = 2048;
//Load upload library and initialize configuration
$this->load->library('upload',$config);
$this->upload->initialize($config);
if($this->upload->do_upload('picture'))
{
$uploadData = $this->upload->data();
$picture = $uploadData['file_name'];
$userData['product_image']=$picture;
}
}
//Pass user data to model
$this->user->update_purifier($id,$userData);
}
//Form for adding user data
$this->data['h']= $this->user->editpurifier($id);
$this->load->view('editwaterpurifier', $this->data, FALSE);
}
Model
public function update_purifier($id,$userData)
{
$this->db->where('id', $id);
$this->db->update('products', $userData);
}
check product image name contain extention . if not add
$ext1 = explode('.',$_FILES['picture']['name']);
$ext2 = array_reverse($ext1);
$extention = strtolower($ext2[0]);
$config['file_name'] = $new_file_name.'.'.$extention;

Related

Images not saving to temporary folder only saved to Database

I want uploads to save to upload/images folder in my project directory but it is not. The upload/images folder is empty while the upload saves to database directory
public function addArticleForm(){
//check whether user upload picture
if(!empty($_FILES['image_path']['upload_path'])){
$config['upload_path'] = 'uploads/images/';
$config['allowed_types'] = 'jpg|jpeg|png|gif|';
$config['file_name'] = $_FILES['image_path']['upload_path'];
//load upload library and initialize configuration
$this->load->library('upload',$config);
$this->upload->initialize($config);
if($this->upload->do_upload('image_path')){
$uploadData = $this->upload->data();
$image_path = $uploadData['file_name'];
}else{
$image_path = '';
}
}
Try This for setting up a dynamic function for image upload
public function upload_images($path,$filename){
$config = array(
'upload_path' => $path,
'allowed_types' => "gif|jpg|png|jpeg",
'overwrite' => TRUE,
'max_size' => "204800", // 20 mb
);
$this->load->library('upload', $config);
$this->upload->initialize($config);
if($this->upload->do_upload($filename)){
$upload_data = $this->upload->data();
$datas = array('file_name' => $upload_data['file_name'], 'file_size'=>$upload_data['file_size'], 'file_type'=>$upload_data['file_type']);
$file_name = $upload_data['file_name'];
$file_size = $upload_data['file_size'];
$file_type = $upload_data['file_type'];
} else {
$datas = array('error' => $this->upload->display_errors());
$this->session->set_flashdata('error_msg',$datas['error']);
}
return $datas;
}
And use above function in another function, Like
public function Add_Customer_Profiles(){
if($this->session->userdata('userid')!="" || null!==$this->session->userdata('userid')){
$custno = $this->input->post('custno',TRUE);
$fname = $this->input->post('fname',TRUE);
$lname = $this->input->post('lname',TRUE);
$email = $this->input->post('email',TRUE);
$mobile = $this->input->post('mobile',TRUE);
$path = './assets/profile_data/';
$img_name = 'customer_img'; // Your file input name
$file_detail = $this->upload_images($path,$img_name);
if(!isset($file_detail['error'])){
//Your Code here
}
}
public function addArticleForm(){
$config['upload_path'] = "./backend/images/";
$config['allowed_types'] = 'gif|jpeg|png|jpg';
$this->path = './backend/images/';
$this->load->library('upload',$config);
$this->upload->initialize($config);
if (!$this->upload->do_upload('file_name'))
{
$error = array('error'=>$this->upload->display_errors());
}else{
$dataUpload = $this->upload->data();
//echo $dataUpload['file_name'];
}
$data = array(
'name'=>$this->input->post('name'),
'image'=>$dataUpload['file_name'],
'created_by'=>$this->input->post('created_by'));
$insertdata = $this->db->insert('table_name',$data);
}

Upload image & video on codeigniter

I have some issue when I insert image and video on database, I am using codeigniter for program.
When I insert both a video and image, my code does not work, but if I upload only one of image/video it works fine.
How can I solve this issue to insert video and image?
My controller code:
private function _do_upload()
{
$config['upload_path'] = './assets/img/sni';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 3000; //set max size allowed in Kilobyte
$config['max_width'] = 1000; // set max width image allowed
$config['max_height'] = 1000; // set max height allowed
$config['file_name'] = round(microtime(true) * 1000); //just milisecond timestamp fot unique name
$this->load->library('upload', $config);
if (!$this->upload->do_upload('image')) //upload and validate
{
$data['inputerror'][] = 'image';
$data['error_string'][] = 'Upload error: ' . $this->upload->display_errors('', ''); //show ajax error
$data['status'] = FALSE;
echo json_encode($data);
exit();
}
return $this->upload->data('file_name');
}
private function _do_upload_video()
{
$config['upload_path'] = './assets/img/sni';
$config['allowed_types'] = 'mp4';
$config['max_size'] = 9000148; //set max size allowed in Kilobyte
$config['file_name'] = uniqid(); //just milisecond timestamp fot unique name
$this->load->library('upload', $config);
if (!$this->upload->do_upload('video')) //upload and validate
{
$data['inputerror'][] = 'video';
$data['error_string'][] = 'Upload error: ' . $this->upload->display_errors('', ''); //show ajax error
$data['status'] = FALSE;
echo json_encode($data);
exit();
}
return $this->upload->data('file_name');
}
public function add_data()
{
$this->_validate();
$data = array(
'kode_process' => $this->input->post('kode'),
'process_name' => $this->input->post('name'),
'remark' => $this->input->post('remark'),
'category' => $this->input->post('category'),
'cycle_time' => $this->input->post('cycle'),
'smv' => $this->input->post('smv'),
'total' => $this->input->post('total'),
);
if (!empty($_FILES['image']['name'])) {
$upload = $this->_do_upload();
$data['image'] = $upload;
}
if (!empty($_FILES['video']['name'])) {
$upload = $this->_do_upload_video();
$data['video'] = $upload;
}
$this->Sni_model->save($data);
echo json_encode(array("status" => TRUE));
Screenshot:

How to upload multiple image in few records in database using codeigniter?

I have a problem with multiple image upload in codeigniter. I want to insert to each field in database.
I've tried to upload single a file but when I change to multiple upload, it's not working.
This is my controller to add the whole data from upload images:
public function addprod(){
$item->gambar_produk = null;
$item->thumb_produk1 = null;
$data = array(
'page' => 'addprod',
'row' => $item
);
$data['title'] = 'Form Add';
$this->load->view('admin/addprod', $data);
}
This is My Controller to add images and this is my first images controller:
$config['upload_path'] = './assets/upload/images/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '2400';
$config['max_width'] = '2024';
$config['max_height'] = '2024';
$this->load->library('upload', $config);
if ($this->upload->do_upload('image')){
$post['image'] = $this->upload->data('file_name');
$this->Model_product->add($post);
redirect('Product/viewprod');
} else {
$error = $this->upload->display_errors();
$this->session->flashdata('error', $error);
redirect('Product/addprod');
}
and this is my second images:
$config2['upload_path'] = './assets/upload/images/';
$config2['allowed_types'] = 'gif|jpg|png|jpeg';
$config2['max_size'] = '2400';
$config2['max_width'] = '2024';
$config2['max_height'] = '2024';
$this->load->library('upload', $config2);
if ($this->upload->do_upload('image2')){
$post['image2'] = $this->upload->data('file_name');
$this->Model_product->add($post);
redirect('Product/viewprod');
} else {
redirect('Product/addprod');
}
this is my model:
public function add($post){
$data = [
'image' => $post['image'],
'image2' => $post['image2']
];
$this->db->insert('tb_prod',$data);
}

The filetype you are attempting to upload is not allowed. while uploading docx, pdf file in codeigniter

while uploading docx, pdf file, The filetype you are attempting to upload is not allowed.
My add function is:-
public function add() {
$this->load->helper('ckeditor'); // for loading ckeditor
$this->load->library('form_validation');
$this->form_validation->set_rules($this->validation_rules);
if ($this->form_validation->run() == TRUE) {
$file_name = '';
if ($_FILES["file"]["size"] > 0) {
$this->load->library('upload', $this->fu_config);
if (!$this->upload->do_upload('file')) {
$this->session->set_flashdata('error', $this->upload->display_errors());
redirect('files/add', 'refresh');
} else {
$file = $this->upload->data();
$file_name = $file['file_name'];
}
}
// preparing for insertion
foreach ($this->validation_rules as $k => $v) {
$fields[] = $v['field'];
}
$data = $this->files_model->array_from_post($fields);
$data['file'] = $file_name;
if ($this->files_model->save($data)) {
$this->session->set_flashdata('success', 'New Event Added Successfully.');
} else {
$this->session->set_flashdata('error', 'sorry, Event cannot be Added.');
}
redirect('files/add/', 'refresh');
} else {
$files = new stdClass();
// Go through all the known fields and get the post values
foreach ($this->validation_rules as $key => $field) {
$files->$field['field'] = set_value($field['field']);
}
}
$data = array(
'method' => 'add',
'main_content' => 'form',
'editData' => $files,
'ckeditor' => array(
'id' => 'description',
'path' => 'js/ckeditor',
)
);
$this->load->view('admin_wrapper', $data);
}
I tried to change system/libraries/Upload.php line 199:
$this->_file_mime_type($_FILES[$field]);
Change that line to:
$this->_file_mime_type($_FILES[$field]); var_dump($this->file_type); die();
I got out put this: -
string(42) "cannot open `' (No such file or directory)"
How can i solve this error ?
make sure your $this->fu_config is correct,
example of the config :
$config['upload_path'] = './uploads/'; //Your file path for upload
$config['allowed_types'] = 'pdf|docx';
$config['max_size'] = 100;
$config['max_width'] = 1024;
$config['max_height'] = 768;

Upload image using codeigniter controller

public function add_software()
{
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<div class="error-form">',
'</div>');
$this->form_validation->set_rules('name', 'Name',
'required|min_length[4]|max_length[25]');
$this->form_validation->set_rules('desc', 'Description', 'required');
$this->form_validation->set_rules('licence', 'Licence.', 'required');
$this->form_validation->set_rules('platform', 'Platform', 'required');
$this->form_validation->set_rules('developers', 'Developer',
'required');
$this->form_validation->set_rules('link', 'Developer Website',
'required');
$this->form_validation->set_rules('cat', 'Category', 'required');
$this->form_validation->set_rules('logo', 'Software Logo', 'required');
if ($this->form_validation->run() == FALSE) {
$this->load->view('admin/includes/header');
$this->load->view('admin/add_software');
$this->load->view('admin/includes/footer');
} else {
//Setting values for tabel columns
$data['name'] = $this->input->post('name');
$data['description'] = $this->input->post('desc');
$data['licence'] = $this->input->post('licence');
$data['platform'] = $this->input->post('platform');
$data['developers'] = $this->input->post('developers');
$data['link'] = $this->input->post('link');
$data['category'] = $this->input->post('cat');
$data['logo'] = $this->input->post('logo');
/////////////////////////////////
//set preferences
$config = array(
'upload_path' => '' .base_url(). '/assets/img/logo/',
'allowed_types' => "gif|jpg|png|jpeg|pdf",
'overwrite' => TRUE,
'max_size' => "4096000",
'max_height' => "1024",
'max_width' => "1024"
);
//load upload class library
$this->load->library('upload', $config);
$this->upload->data();
$this->upload->do_upload();
/////////////////////////////////
//Transfering data to Model
$this->insert_model->form_insert($data);
$data['message'] = 'Data Inserted Successfully';
//Loading View
redirect('admin/view_softwares', 'refresh');
}
}
This is my codeigniter controller function. Using this function, I can add data to the database. But, I am unable to upload image. Even upload_files is enabled in php.ini. I tried resolving this problem all day long, but all in vain.
I have not used enctype in my html form because when I use enctype, I get the below error:
undefined Index filename
Friend i wil give code that i have done for uploading image.This surely work.
You can refer this.Any doubt,please ask,i wil help you
public function uploadImage() {
$this->load->helper(array('form', 'url'));
$config['upload_path'] = 'assets/images/b2bcategory';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1000';
$config['max_width'] = '2024';
$config['max_height'] = '1768';
$config['width'] = 75;
$config['height'] = 50;
if (isset($_FILES['catimage']['name'])) {
$filename = "-" . $_FILES['catimage']['name'];
$config['file_name'] = substr(md5(time()), 0, 28) . $filename;
}
$config['overwrite'] = TRUE;
$config['remove_spaces'] = TRUE;
$field_name = "catimage";
$this->load->library('upload', $config);
if ($this->input->post('selsub')) {
if (!$this->upload->do_upload('catimage')) {
//no file uploaded or failed upload
$error = array('error' => $this->upload->display_errors());
} else {
$dat = array('upload_data' => $this->upload->data());
$this->resize($dat['upload_data']['full_path'], $dat['upload_data']['file_name']);
}
$ip = $_SERVER['REMOTE_ADDR'];
if (empty($dat['upload_data']['file_name'])) {
$catimage = '';
} else {
$catimage = $dat['upload_data']['file_name'];
}
$data = array(
'ctg_image' => $catimage,
'ctg_dated' => time()
);
$this->b2bcategory_model->form_insert($data);
}
}
Your path is wrong.
'upload_path' => '' .base_url(). '/assets/img/logo/',
Note: Make sure your upload path "assets folder" is in the main directory out side of
application folder. You also may need to set folder permissions.
Try
'upload_path' => FCPATH . 'assets/img/logo/'
or just
'upload_path' => './assets/img/logo/'
To access file info after upload success preferences
$image_info = $this->upload->data();
echo $image_info['file_name'];
Or Through Model
$this->insert_model->form_insert($data, $image_info = array());

Categories