codeigniter Missing value file name in db - php

I have a problem with update in my db the value of file upload
My controller
public function c_piloti_update()
{
// Modifica i valori scheda pilota
$id= $this->input->post('id');
$data = array(
'nominativo' => $this->input->post('nominativo'),
'fotografia' => $this->input->post('fotografia')
);
$config['upload_path'] = './uploads/avatar/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 1000;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('fotografia'))
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else
{
$this->load->model('alfa/alfa_model');
$this->piloti_model->alfa_update($id,$data);
redirect('alfa/alfa-edit/'.$id);
}
}
My Model
public function alfa_update($id, $data)
{
$this->db->where('id', $this->input->post('id'));
$this->db->update('register', $data);
}
The attachment is saved, but in the DB i haven't the file name.
thanks

Get File details using this : $this->upload->data();
if ( ! $this->upload->do_upload('fotografia'))
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else
{
$data['image_name '] = $this->upload->data('file_name');
//here image_name is column name in table
$this->load->model('alfa/alfa_model');
$this->piloti_model->alfa_update($id,$data);
redirect('alfa/alfa-edit/'.$id);
}
see this :
https://www.codeigniter.com/user_guide/libraries/file_uploading.html#the-upload-directory
Change model method alfa_update() to this :
public function alfa_update($id, $data)
{
$this->db->where('id',$id);
$this->db->update('register', $data);
}

Related

Codeigniter image upload validation with controller and function

I have a small issue on this code for the to add a product on my system I am using V_add_pro function, when the image upload part is coming I am facing some issues to validate, so that I used a different function to validate through callback, its not working. the main issue is i am saving the image name only to the database, so how to return the image name only?
public function v_add_pro(){
$this->load->model('B_menu');
$data = array('status' => false, 'msg' => array());
$this->load->library('form_validation');
$this->form_validation->set_rules("pro_name", "name needed", "trim|required");
$this->form_validation->set_rules("pro_price", "price needed", "trim|required");
$this->form_validation->set_rules("pro_desc", "description is needed", "trim|required");
$this->form_validation->set_rules("dropzone", "select an image", "trim|required|callback_validate_image");
$this->form_validation->set_rules("pro_cat", "please select a category","callback_check_default|trim|required");
$this->form_validation->set_error_delimiters('<p class="text-danger">','</p>');
if ($this->form_validation->run()) {
$data['status'] = true;
$pro_data = array(
'pname' => $this->input->post('pro_name'),
'pprice' => $this->input->post('pro_price'),
'pdesc' => $this->input->post('pro_desc'),
'pimage' =>$this->input->post('dropzone'),
'catid' => $this->input->post('pro_cat'),
);
$this->B_menu->add($pro_data);
} else {
foreach ($_POST as $key => $value) {
$data['msg'][$key] = form_error($key);
}
}
echo json_encode($data);
}
//to check the selected value for category
function check_default($post_string)
{
return $post_string == '0' ? FALSE : TRUE;
}
//to upload the image to the correct path
public function validate_image() {
$config = array(
'allowed_types'=>'jpg|png|gif',
'upload_path'=> realpath(APPPATH . '../skin/images'),
'max_size'=>1
);
$this->load->library('upload', $config);
if (!$this->upload->do_upload('dropzone'))
{
$this->form_validation->set_message('validate_image',$this->upload->display_errors());
} else {
$file_info = $this->upload->data();
$img = $file_info['file_name'];
return $img;
}
}
public function validate_image()
{
/* Start: Image Uploading */
$baseurl = $this->config->base_url();
$profile_photo = "";
$profile_path = '';
$config['upload_path'] = './assets/profile_pictures';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '0';
$config['max_width'] = '0';
$config['max_height'] = '0';
if (!empty($_FILES))
{
$this->file_ext = pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION);
$this->file_name = time() . "." . $this->file_ext;
$config['file_name'] = $this->file_name;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (!$this->upload->do_upload('image', FALSE))
{
echo $this->upload->display_errors();
$this->form_validation->set_message('checkdoc', $data['error'] = $this->upload->display_errors());
if ($_FILES['image']['error'] != 4)
{
return false;
}
}
else
{
$profile_photo = $this->file_name;
$profile_path = $baseurl . "assets/profile_pictures";
return $profile_photo;
}
}
/* End: Image Uploading */
To add-ons I am just saving file name as current timestamp thats it. Also return $imagename and also try to echo this
echo $this->upload->display_errors();

Error on uploading CSV File in Codeigniter

Hi I am trying to import an CSV into MYSQL database using A CI Library. But I get this error in Controller that file type is now allowed
Controller:
<?php
class Csv extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('csv_model');
$this->load->library('csvimport');
}
function index() {
$data['addressbook'] = $this->csv_model->get_addressbook();
$this->load->view('csvindex', $data);
}
function importcsv() {
$data['addressbook'] = $this->csv_model->get_addressbook();
$data['error'] = ''; //initialize image upload error array to empty
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'csv';
$config['max_size'] = '1000';
$this->load->library('upload', $config);
// If upload failed, display error
if (!$this->upload->do_upload()) {
$data['error'] = $this->upload->display_errors();
$this->load->view('csvindex', $data);
} else {
$file_data = $this->upload->data();
$file_path = './uploads/'.$file_data['file_name'];
if ($this->csvimport->get_array($file_path)) {
$csv_array = $this->csvimport->get_array($file_path);
foreach ($csv_array as $row) {
$insert_data = array(
'firstname'=>$row['firstname'],
'lastname'=>$row['lastname'],
'phone'=>$row['phone'],
'email'=>$row['email'],
);
$this->csv_model->insert_csv($insert_data);
}
$this->session->set_flashdata('success', 'Csv Data Imported Succesfully');
redirect(base_url().'csv');
//echo "<pre>"; print_r($insert_data);
} else
$data['error'] = "Error occured";
$this->load->view('csvindex', $data);
}
}
}
/*END OF FILE*/
The Model :
<?php
class Csv_model extends CI_Model {
function __construct() {
parent::__construct();
}
function get_addressbook() {
$query = $this->db->get('addressbook');
if ($query->num_rows() > 0) {
return $query->result_array();
} else {
return FALSE;
}
}
function insert_csv($data) {
$this->db->insert('addressbook', $data);
}
}
/*END OF FILE*/
I am trying to Import A CSV using PHP Codeigniter. Now I am getting an error that
The filetype you are attempting to upload is not allowed.
So as you can see i have kept the allowed file type = csv then also this issue is coming. So please help. Thanks
Instead of changing the mime type you can go with the CALLBACK function
This will add more portability too...
function importcsv() {
$data['addressbook'] = $this->csv_model->get_addressbook();
$data['error'] = ''; //initialize image upload error array to empty
$this->form_validation->set_rules('uploaded_file','Uploaded file', 'trim|callback_chk_attachment');
if($this->form_validation->run()){
$config['upload_path'] = './uploads/';
$config['allowed_types'] = '*';
$config['max_size'] = '1000';
$this->load->library('upload', $config);
// If upload failed, display error
if (!$this->upload->do_upload()) {
$data['error'] = $this->upload->display_errors();
$this->load->view('csvindex', $data);
} else {
$file_data = $this->upload->data();
$file_path = './uploads/'.$file_data['file_name'];
if ($this->csvimport->get_array($file_path)) {
$csv_array = $this->csvimport->get_array($file_path);
foreach ($csv_array as $row) {
$insert_data = array(
'firstname'=>$row['firstname'],
'lastname'=>$row['lastname'],
'phone'=>$row['phone'],
'email'=>$row['email'],
);
$this->csv_model->insert_csv($insert_data);
}
$this->session->set_flashdata('success', 'Csv Data Imported Succesfully');
redirect(base_url().'csv');
//echo "<pre>"; print_r($insert_data);
} else
$data['error'] = "Error occured";
$this->load->view('csvindex', $data);
}
}
Call Back function for File Upload validation:
public function chk_attachment() // callback validation for check the attachment extension
{
$file_type = array('.csv');
if(!empty($_FILES['uploaded_file']['name']))
{
$ext = strtolower(strrchr($_FILES['uploaded_file']['name'],"."));
if(in_array($ext,$ext_array))
{
return true;
}
else
{
$this->form_validation->set_message('chk_attachment','Attachment allowed only csv');
return false;
}
}
{
$this->form_validation->set_message('chk_attachment','image field is required');
return false;
}
}

uploading csv not worked properly

When I upload the csv file only first data is uploaded.
This is my controller.
public function uploadbusinesstype()
{
$config['upload_path'] = APPPATH.'/assets/upload/';
$config['allowed_types'] = 'csv';
$config['max_size'] = '5000';
$replace = '"';
$this->load->library('upload', $config);
$this->load->database();
if ( ! $this->upload->do_upload('file_name'))
{
$error = array('error' => $this->upload->display_errors());
$this->session->set_flashdata('msg_excel','Choose a .csv file to upload');
redirect(base_url().'admin/businesstype/managecategories');
}
else
{
$data = array('upload_data' => $this->upload->data());
$userfile = $data['upload_data']['file_name'];
$this->load->library('csvreader');
$upload_data = $this->upload->data();
$file = $upload_data['full_path'];
$file_name = $upload_data['userfile'];
$data = $this->csvreader->parse_file($file);
foreach($data as $row)
{
if($row['main_type_name']=='')
{
$this->session->set_flashdata('msg_excel','Please check the Type Name.It is empty');
redirect(base_url().'admin/businesstype/managecategories');
}
else if(!preg_match("/^[a-zA-Z& ]+$/",$row['main_type_name']))
{
$this->session->set_flashdata('msg_excel','Please check the Type Name.Only alphabets are allowed');
redirect(base_url().'admin/businesstype/managecategories');
}
else
{
$results_array = array(
'type_id' => $row['type_id'],
'main_type_name' => $row['main_type_name']
);
$this->load->model('admin/businesstype_model');
$this->businesstype_model->catupload($results_array);
}
$success_message='Successfully Upload!';
$this->session->set_flashdata('success_message',$success_message);
redirect(base_url().'admin/businesstype/managecategories');
}
}
}
This is my model
function catupload($results_array)
{
$this->db->insert('cr_main_shop_type', $results_array);
}
Here There is main type name is there.when i gave accessories,watch and phone as main type name in csv. only first value will enter in the database.
Try this..
Add all values to array and pass array to model "businesstype_model"
$results_array=array();
foreach($data as $row)
{
if($row['main_type_name']=='')
{
$this->session->set_flashdata('msg_excel','Please check the Type Name.It is empty');
redirect(base_url().'admin/businesstype/managecategories');
}
else if(!preg_match("/^[a-zA-Z& ]+$/",$row['main_type_name']))
{
$this->session->set_flashdata('msg_excel','Please check the Type Name.Only alphabets are allowed');
redirect(base_url().'admin/businesstype/managecategories');
}
else
{
//Store values to array
$results_array[] = array(
'type_id' => $row['type_id'],
'main_type_name' => $row['main_type_name']
);
}
if(count($results_array))
{
$this->load->model('admin/businesstype_model');
$this->businesstype_model->catupload($results_array);
}
$success_message='Successfully Upload!';
$this->session->set_flashdata('success_message',$success_message);
redirect(base_url().'admin/businesstype/managecategories');
}
else
{
$results_array = array(
'type_id' => $row['type_id'],
'main_type_name' => $row['main_type_name']
);
$this->load->model('admin/businesstype_model');
$this->businesstype_model->catupload($results_array);
}
}
$success_message='Successfully Upload!';
$this->session->set_flashdata('success_message',$success_message);
redirect(base_url().'admin/businesstype/managecategories')
}
}
i got it by using this method

CSV upload not working correctly

In this, I want to upload csv files. Validation is not correct but without validation its working.
This is my controller:
public function uploadstatetype()
{
$config['upload_path'] = APPPATH.'/assets/upload/';
$config['allowed_types'] = 'csv';
$config['max_size'] = '5000';
with = ' ';
$replace = '"';
$this->load->library('upload', $config);
$this->load->database();
if ( ! $this->upload->do_upload('file_name'))
{
$error = array('error' => $this->upload->display_errors());
$this->managestate($error);
}
else
{
//Insert file info into database
$data = array('upload_data' => $this->upload->data());
$userfile = $data['upload_data']['file_name'];
$this->load->library('csvreader');
$upload_data = $this->upload->data();
$this->load->library('csvreader');
$file = $upload_data['full_path'];
$file_name = $upload_data['userfile'];
$data = $this->csvreader->parse_file($file);
foreach($data as $row)
{
if(($row['state_id']=='')||($row['state_name']==''))
{
$this->session->set_flashdata('msg_excel','Please check the details in the file, some details are empty.');
redirect(base_url().'admin/businesslocation/managestate');
}
else
{
$results_array = array(
'state_id' => $row['state_id'],
'state_name' => $row['state_name']
);
$this->load->model('admin/businesslocation_model');
$this->businesslocation_model->stateupload($results_array);
$success_message='Successfully Upload!';
$this->session->set_flashdata('success_message',$success_message);
redirect(base_url().'admin/businesslocation/managestate');
}
}
}
}
And this is my model:
function stateupload($results_array)
{
$this->db->insert('cr_state', $results_array);
}
For eg state_id=1 and state_name="kerala". Then insert is working,when condition is not checked. But when if condition is given it's not working.
Change your condition like this and try
if(empty($row['state_id'])||(empty($row['state_name']))){

Duplicate values insert into the database in Codeigniter

I am inserting some data into the database in my Codeigniter controller
function addsoothingmemory() {
$config['upload_path'] = './uploads2/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '500000';
$config['max_width'] = '100024';
$config['max_height'] = '100768';
$this->upload->initialize($config);
if (!$this->upload->do_upload()) {
$error = array('error' => $this->upload->display_errors());
$this->load->view('soothing', $error);
} else {
$imagedata = $this->upload->data();
$data = array(
'image_path' => $imagedata['file_name'],
'title' => $this->input->post('title'),
'user_id' => $this->session->userdata("user_id"),
'feelingrate' => $this->session->userdata("feelingrate"),
'description' => $this->input->post('description')
);
$query = $this->favourite_db->getsoothingcount() + 1;
$this->favourite_db->add_soothingmemory($data);
$count = array('soothingcount' => $query);
$this->favourite_db->updateusercount($count);
$this->load->view('positivepast', $data);
}
}
Model
function add_soothingmemory($data) {
$this->load->database();
$this->db->insert('soothingmemory', $data);
$this->set_session();
return $data;
}
The problem is when the value is inserted into the database it inserts three times creating multiple/duplicate rows.
I had the same problem,
anyhow, in your model,
$this->db->limit(1);
$this->db->insert('soothingmemory', $data);
that way duplicates will be avoided.
Hope it helps
Make the following changes:
function addsoothingmemory() {
$config['upload_path'] = './uploads2/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '500000';
$config['max_width'] = '100024';
$config['max_height'] = '100768';
$this->upload->initialize($config);
if (!$this->upload->do_upload()) {
$error = array('error' => $this->upload->display_errors());
$this->load->view('soothing', $error);
} else {
$imagedata = $this->upload->data();
$data = array(
'image_path' => $imagedata['file_name'],
'title' => $this->input->post('title'),
'user_id' => $this->session->userdata("user_id"),
'feelingrate' => $this->session->userdata("feelingrate"),
'description' => $this->input->post('description')
);
$query = $this->favourite_db->getsoothingcount() + 1;
$save = $this->favourite_db->add_soothingmemory($data);
// Move this code into the model
// $count = array('soothingcount' => $query);
if($save == true){
$this->favourite_db->updateusercount($query);
$this->load->view('positivepast', $data);
}else{
//do something else
}
}
}
In the model
function add_soothingmemory($data) {
$this->load->database();
$save = $this->db->insert('soothingmemory', $data);
//This condition will check, if the $data was save
if($save == true){
//If is save, it will set the session and return true
$this->set_session();
return true;
}else{
return false;
}
}

Categories