Codeigniter将CSV上传到数据库

时间:2013-06-17 13:09:15

标签: php database codeigniter csv

提前感谢你给我的帮助,我会解释我的情况 基于教程

File uploading

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

Form creating insert data

我正在尝试创建一个页面,允许我上传CSV文件,解析此文档并将数据插入我的数据库。 我已经写了这段代码:

<?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);


        } 
    } 
 }
}
?>

我使用谷歌浏览器并在我尝试使用index.php / upload中的条目时出现此错误:HTTP错误500(内部服务器错误)。 我在Codeigniter的根目录中的config / database.php中声明了我的数据库。 我正在尝试将解决方案联系到我的问题,但我仍然没有弄清楚我错在哪里。 谢谢。

这是我的库/ 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;
}
} 

1 个答案:

答案 0 :(得分:0)

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

!符号前面应该有$,因此子句部分成为错误部分,而在其他部分则可以继续。

do_upload function ()

这应该是

function do_upload()

太多空格会破坏语法,这可能会导致错误。