thank you in advance for the help you give me, I'll explain my situation.
Based on tutorial
Forum : Just a nice csv upload and populate the database function
I'm trying to make a page that allows me to upload a CSV file, parse this document and inserting data into my database. I've written up to now this code:
<?php
class Upload extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
}
function index()
{
$this->load->view('upload_form', array('error' => ' ' ));
}
function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'csv|tsv';
$config['max_size'] = '1024';
$replace = '"';
$with = ' ';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else
{
//Insert CSV Data into database
$data = array('upload_data' => $this->upload->data());
$this->load->library('csvreader');
$data['csvData'] = $this->csvreader->parse_file($data);
$this->load->database();
$data = array(
'Parola chiave'=>$this->input->post('Parola chiave'),
'Concorrente'=>$this->input->post('Concorrente'),
'Motore di ricerca'=>$this->input->post('Motore di ricerca'),
'Posizione'=>$this->input->post('Posizione'),
'Pagina web'=>$this->input->post('Pagina web'),
'Modifiche'=>$this->input->post('summary'),
);
$this->db->insert('data',$data);
}
$this->load->view('upload_success', $data);
}
}
}
}
?>
I use google chrome and gives me this error: HTTP Error 500 (Internal Server Error) when I try to entries in index.php / upload. I have declared my database in config / database.php in the root of Codeigniter. I'm trying to network the solution to my problem but I still have not figured out where I'm wrong. Thank you.
This is my library/csvreader.php
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class CSVReader {
var $fields; /** columns names retrieved after parsing */
var $separator = ','; /** separator used to explode each line */
/**
* Parse a text containing CSV formatted data.
*
* @access public
* @param string
* @return array
*/
function parse_text($p_Text) {
$lines = explode("\n", $p_Text);
return $this->parse_lines($lines);
}
/**
* Parse a file containing CSV formatted data.
*
* @access public
* @param string
* @return array
*/
function parse_file($p_Filepath) {
$lines = file($p_Filepath);
return $this->parse_lines($lines);
}
/**
* Parse an array of text lines containing CSV formatted data.
*
* @access public
* @param array
* @return array
*/
function parse_lines($p_CSVLines) {
$content = FALSE;
foreach( $p_CSVLines as $line_num => $line ) {
if( $line != '' ) { // skip empty lines
$elements = split($this->separator, $line);
if( !is_array($content) ) { // the first line contains fields names
$this->fields = $elements;
$content = array();
} else {
$item = array();
foreach( $this->fields as $id => $field ) {
if( isset($elements[$id]) ) {
$item[$field] = $elements[$id];
}
}
$content[] = $item;
}
}
}
return $content;
}
}
$
– dianuj 21 hours ago