Codeigniter Library to Read CSV File
Codeigniter Library to Read CSV File
http://thebizztech.com/2011/04/26/codeigniter-library-to-read-csv-file/
// -------------------------------Library--------------------------- //
//application/libraries/getcsv.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Getcsv {
private $file_path = "";
private $handle = "";
public function set_file_path($file_path)
{
$this->file_path = $file_path;
return $this;
}
private function get_handle()
{
$this->handle = fopen($this->file_path, "r");
return $this;
}
private function close_csv()
{
fclose($this->handle);
return $this;
}
//this is the most current function to use
public function get_array()
{
$this->get_handle();
$row = 0;
while (($data = fgetcsv($this->handle, 0, "\t")) !== FALSE)
{
if($row == 0)
{
foreach ($data as $key => $value)
{
$title[$key] = trim($value); //this extracts the titles from the first row and builds array
}
}
else
{
$new_row = $row - 1; //this is needed so that the returned array starts at 0 instead of 1
foreach($title as $key => $value) //this assumes there are as many columns as their are title columns
{
if (trim($data[$key]) != '') {
$result[$new_row][$value] = trim($data[$key]);
}
}
}
$row++;
}
$this->close_csv();
return $result;
}
// --------------------------------Main Functions Above--------------------------- //
//This function is being left in incase I ever need it
function get_csv_array()
{
$row = 0;
if (($handle = fopen($this->file_path, "r")) !== FALSE)
{
while (($data = fgetcsv($handle, 1000, "\t")) !== FALSE)
{
$final_array[$row] = $data;
$row++;
}
fclose($handle);
}
return $final_array;
}
} //End of class
//Here is the end of the getcsv.php class
// -------------------------------Controller:uploadfile.php----------------------- //
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Uploadfile extends CI_Controller{
public function index(){
//$this->load->view('include/header');
$file = file_get_contents ($_FILES['upload']['tmp_name']);
$temp_folder = getcwd().'/temp/';
if(!is_dir($temp_folder)){
mkdir($temp_folder);
}
if((!empty($_FILES["upload"])) && ($_FILES['upload']['error'] == 0)) {
$file_category = $_POST['file_category'];
$filename = basename($_FILES['upload']['name']);
$this->$file_category($_FILES['upload']['tmp_name']);
$ext = substr($filename, strpos($filename,'.')+1);
if($ext=='csv'){
$csvfile_path = $temp_folder.$filename;
if(!file_exists($csvfile_path)){
if(move_uploaded_file($_FILES['upload']['tmp_name'],$csvfile_path)){
$this->$file_category($csvfile_path);
}
else {
echo "Error: A problem occurred during file upload!";
}
}
else {
echo "Error: File ".$_FILES["uploaded_file"]["name"]." already exists";
}
}
else{
echo "Please upload file in CSV format";
}
}
$this->load->view('uploadfile');
//$this->load->view('include/footer');
}
public function getcsv_team($filepath)
{
$this->load->library('getcsv');
$team_array = array();
$data = $this->getcsv->set_file_path($filepath)->get_csv_array();
$i = 0;
echo '<pre>';
echo $filepath;
print_r($data);
die;
foreach ($data as $key => $val) {
if($key > 1){
$country_name = $val[0];
$country_id = $this->signup_model->getcountryid_byname($country_name);
$league = $val[1];
$league_level = $val[2];
$team_name = $val[3];
$team_shortname = $val[4];
$city = $val[5];
$is_exists = $this->signup_model->isteam_alreadyexists($team_name,$country_id);
if(! $is_exists ){
$team = array(
'country_id' => $country_id,
'league' => $league,
'league_level' => $league_level,
'team_name' => $team_name,
'team_shortname' => $team_shortname,
'city' => $city,
);
array_push($team_array,$team);
}
}
}
if(! empty($team_array) ){
$result = $this->signup_model->insertbatch('ht_team_master',$team_array);
if($result){
echo "Inserted Successfully.";
unlink($filepath);
}
else{
die(mysql_error());
}
}
else{
echo "No new data found to insert.";
}
}
}

Post a Comment