0

thank you in advance for the help you give me, I'll explain my situation.
Based on tutorial

File uploading

Forum : Just a nice csv upload and populate the database function

Form creating insert data

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';
    $config['max_size']  = '5000';
    $with = ' ';
    $replace = '"';

    $this->load->library('upload', $config);
    $this->load->database();

    if ( ! $this->upload->do_upload())
    {
        $error = array('error' => $this->upload->display_errors());
        $this->load->view('upload_form', $error);
    }
    else
    {
//Insert file info into database
$data = array('upload_data' => $this->upload->data());
$userfile = $data['upload_data']['file_name'];
$this->load->library('csvreader');
$filePath1 = './uploads/';
$filePath2 = $data['upload_data']['file_name'];
$filePath = $filePath1 . $filePath2;
$data['csvData'] = $this->csvreader->parse_file($filePath);
foreach($data['csvData'] as $cd){
    $results_array = array(
                           'Parolachiave' => $cd['Parola chiave'],
                           'Concorrente' => $cd['Concorrente'],
                           'Motorediricerca' => $cd['Motore di ricerca'],
                           'Posizione' => $cd['Posizione'],
                           'Paginaweb' => $cd['Pagina web'],
                           'Modifiche' => $cd['Modifiche']
                           );        
           $this->db->set($results_array);
           $this->db->insert('data', $results_array);


        } 
    } 
 }
}
?>

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;
}
} 
5
  • 1
    Why there is a space after $ Commented Jun 17, 2013 at 13:19
  • Watch your php logs and give us the last fatal errors Commented Jun 17, 2013 at 13:19
  • 1
    @dianuj I would say, why is there spaces everywhere in the code... Commented Jun 17, 2013 at 13:21
  • 1
    @Brewal exactly too much spaces Commented Jun 17, 2013 at 13:22
  • Sorry for the much spaces and sorry for the question i'm a newbie in programming, but where i can see the logs in phpmyadmin? thanks Commented Jun 17, 2013 at 14:34

1 Answer 1

0
if ($ this-> upload-> do_upload ())

There should be a ! before the $ sign, so that clause part becomes the error part, and at the else part you can go on.

And

do_upload function ()

This should be

function do_upload()

Also too much spaces that breaks syntax, which possibly causes the error.

Sign up to request clarification or add additional context in comments.

10 Comments

I edit my code, im sorry i used google translate and it changed my code.
You declared a function inside a function, that is what causing the error, function entry_insert(){ delete the function declaration
Thanks the problem was precisely the function within the function. Now I log in on the upload page but when I try to load a csv file comes out the following message: "The filetype you are attempting to upload is not allowed". And then if I go to see the table data of the db is empty. Thank u.
@ValeBoccaccio Which version of CodeIgniter are you using, and are you on a Windows based hosting?
I use the last versione of Codeigniter 2.1.3, i work on windows 7 and my web server is: MySQL Server: Localhost via UNIX socket Versione MySQL: 5.1.61-0ubuntu0.11.04.1 Set di caratteri MySQL: UTF-8 Unicode (utf8) Web server Apache/2.2.17 (Ubuntu) Versione MySQL client: 5.1.61 Estensioni PHP: mysqli phpMyAdmin Informazioni sulla versione: 3.3.10deb1
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.