I am developing an website in php where user can upload csv file. after clicking 'upload' button, file is renamed and saved in the format 'upload'.time(). I need to show the contents of the uploaded file just after uploading the file. So far I have been able to upload file correctly and I have tested with a static filename and have been able to read csv file perfectly. but when I am trying to get recently uploaded file name just after uploading I am not getting anywhere. searched through the whole site but didn't get any solution that worked for me. I am using codeigniter 2.1.0 .
Controller file:
<?php
class Csv_ci extends CI_Controller {
function is_logged() {
$is_logged_in = $this->session->userdata('is_logged_in');
if (!isset($is_logged_in) || $is_logged_in != true)
return false;
else
return true;
}
function index() {
if ($this->is_logged() == TRUE) {
$this->load->model('Csv_model');
if ($this->input->post('upload')) {
$name = $this->Csv_model->do_upload();
//print_r($name);
if (isset($name)) {
$filePath = "localhost/map_ci/csv/" . $name;
//$filePath = './csv/bank_data.csv';
$row = 1;
if (($handle = fopen($filePath, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 0, ",")) !== FALSE) {
$num = count($data);
$file_data[$row]['district_name'] = $data[1];
$file_data[$row]['bank_name'] = $data[2];
$file_data[$row]['area_name'] = $data[3];
$file_data[$row]['address'] = $data[4];
$row++;
}
fclose($handle);
}
$this->load->view('csvshow', array('file_data' => $file_data));
}
}
}
else $this->load->view('invalid_member');
}
}
Model file:
<?php
class Csv_model extends CI_Model {
var $gallery_path;
var $gallery_path_url;
function Csv_model() {
parent::__construct();
$this->gallery_path = realpath(APPPATH . '../csv');
$this->gallery_path_url = base_url() . 'csv/';
}
function do_upload() {
$config = array(
'upload_path' => $this->gallery_path,
'allowed_types' => 'text/csv|csv|text/x-comma-separated-values|text/comma-separated-values|application/x-csv|text/x-csv|text/csv|application/csv|',
'max_size' => '5000',
'file_name' => 'upload' . time()
);
$this->load->library('upload', $config);
if (!$this->upload->do_upload())
echo $this->upload->display_errors();
else {
$file_info = $this->upload->data();
$csvfilepath = "csv/" . $file_info['file_name'];
$this->addfromcsv($csvfilepath);
$filename = $file_info['file_name'];
// print_r($filename);
return $filename;
}
}
}
to retrieve file name I am doing file_info['file_name'].
Please help me find a solution.
Update:
My current problem has been solved after removing $csvfilepath = "csv/" . $file_info['file_name'];
$this->addfromcsv($csvfilepath); these two lines from my code.Now its returning uploaded file name.yet to check whether fully working or not.
Update 2: code functioning well thanks all for your help.