【问题标题】:Codeigniter Upload CSV to DatabaseCodeigniter 将 CSV 上传到数据库
【发布时间】:2013-06-13 10:49:19
【问题描述】:

先谢谢你给我的帮助,我会解释一下我的情况。
基于教程

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


        } 
    } 
 }
}
?>

我使用谷歌浏览器并给我这个错误:HTTP Error 500 (Internal Server Error) 当我尝试在 index.php/upload 中输入条目时。 我已经在 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;
}
} 

【问题讨论】:

  • 为什么$后面有个空格
  • 查看您的 php 日志并告诉我们最后的致命错误
  • @dianuj 我会说,为什么代码中到处都是空格...
  • @Brewal 空格太多
  • 对不起,我是编程新手,但我在哪里可以看到 phpmyadmin 中的日志?谢谢

标签: php database codeigniter csv


【解决方案1】:
if ($ this-> upload-> do_upload ())

$ 符号之前应该有一个!,这样子句部分就成为错误部分,而在 else 部分你可以继续。

do_upload function ()

这应该是

function do_upload()

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

【讨论】:

  • 我编辑了我的代码,对不起,我使用了谷歌翻译,它改变了我的代码。
  • 你在函数内部声明了一个函数,这是导致错误的原因,function entry_insert(){删除函数声明
  • 谢谢问题正是函数中的函数。现在我登录上传页面,但是当我尝试加载 csv 文件时出现以下消息:“不允许您尝试上传的文件类型”。然后如果我去看db的表数据是空的。谢谢你。
  • @ValeBoccaccio 您使用的是哪个版本的 CodeIgniter,您使用的是基于 Windows 的主机吗?
  • 我使用 Codeigniter 2.1.3 的最后一个版本,我在 Windows 7 上工作,我的 Web 服务器是: MySQL 服务器:Localhost via UNIX socket Versione MySQL:5.1.61-0ubuntu0.11.04.1 设置di caratteri MySQL: UTF-8 Unicode (utf8) Web server Apache/2.2.17 (Ubuntu) Versione MySQL客户端: 5.1.61 Estensioni PHP: mysqli phpMyAdmin Informazioni sulla versione: 3.3.10deb1
猜你喜欢
  • 2018-09-03
  • 1970-01-01
  • 2016-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-09
相关资源
最近更新 更多