如何使用codeigniter将csv文件导入mysql

时间:2014-02-08 07:16:28

标签: php mysql codeigniter csv

我想使用codeigniter将csv导入mysql。 这是我的源代码。

视图

<?php $this->load->view("admin/v_header");?>
<?php $this->load->view("admin/v_top_menu");?>
<?php $this->load->view("admin/v_sidebar");?>

<div class="content">
    <div class="header">
        <h1 class="page-title">Import Data Dosen</h1>
    </div>
    <?php $this->load->view("admin/v_alert_msg");?>

    <form class="form-horizontal" action="<?php echo base_url();?>admin/save_dosen" method="POST" enctype="multipart/form-data">
        <fieldset>
            <legend>Import Data Dosen</legend>
            <div class="control-group">
                <label class="control-label"><b>Pilih File :</b></label>
                <div class="controls">
                    <div class="input-prepend">
                        <span class="add-on"><i class="icon-barcode"></i></span>
                        <input type="file" name="csv_dosen" id="csv_dosen"/>
                    </div>
                </div>
            </div>

            <div class="control-group">
                <div class="controls">
                    <button type="submit" class="btn btn-primary">
                        <i class="icon-ok icon-white"></i>Save
                    </button>
                </div>
            </div>
        </fieldset>
    </form>
</div>
<?php $this->load->view("admin/v_footer");?>

文库

<?php

error_reporting(0);

    class Csv_impot {

        var $csvfile, $delimitatore, $nometable;
        var $_FIELD;

        function csv_import($cf = "", $del = "", $nt = "") {
            $this->csvfile = $cf;
            $this->delimitatore = $del;
            $this->nometable = $nt;
        }

        function export() {
            $csvhandle = file($this->csvfile);
            $field = explode($this->delimitatore, $csvhandle[0]);
            $kolom = "";
            foreach ($field as $array_kolom) {
                $kolom.="`" . trim($array_kolom) . "`,";
            }
            $kolom = trim(substr($kolom, 0, -1));
            //echo $kolom;
            for ($i = 1; $i <= count($csvhandle); $i++) {
                $valori = explode($this->delimitatore, $csvhandle[$i]);
                $values = "";
                foreach ($valori as $val) {
                    $val = trim($val);
                    if (eregi("NULL", $val) == 0)
                        $values.="'" . addslashes($val) . "',";
                    else
                        $values.="NULL,";
                }
                $values = trim(substr($values, 0, -1));
                $query = "INSERT INTO " . $this->nometable . "(" . $kolom . ") values(" . trim($values) . ");";
                $QUERY[$i] = $query;
            }
            return $QUERY;
        }

    }

控制器

function import_dosen()
    {
        $this->data['title']="Import Data Dosen";
        $this->load->view("admin/v_import_dosen",  $this->data);

    }
    function save_dosen()
    {

        if(isset($_FILES['csv_dosen']['name']))
        {
            $csv_dosen=$_FILES['csv_dosen']['name'];
            $handle = fopen($csv_dosen,"r");
            $this->load->library('csv_import');
            $csv=new csv_import(".$handle.",",","dosen");
            $query=$csv->export();
            $this->m_dosen->eksekusi($query);
//            $check_file=  explode(".", $csv_dosen);
//            if(strtolower($check_file[1])=="csv")
//            {
//                $csv_dosen=$_FILES['csv_dosen']['temp_name'];
//                $handle = fopen($csv_dosen,"r");
//                while (($data = fgetcsv($handle, 1000,",")) !== FALSE)
//                {
//                    echo "haha";
//                }
//            }
//            else{echo "bukan file csv";}
            //$handle = fopen($csv_dosen,"r"); 
            //$csv_dosen_type=$_FILES['csv_dosen']['type'];
            //$csv_dosen_size=$_FILES['csv_dosen']['size'];
        }
        //echo $handle;
    }

模型

<?php
class M_dosen extends CI_model
{
    function __contruct()
    {
        parent::__construct();
    }

    function eksekusi($query)
    {
    //echo "<br/>";
    //echo count($query);
        for($i=1;$i<count($query);$i++)
        {
            $this->db->query($query[$i]);
            //echo $query[$i];
            //echo "<br/>"
        }
    }
}
?>

当我运行此代码时,错误显示[function.fopen]:无法打开流:没有这样的文件或目录。 我是如何解决这个问题的? 我希望你能帮忙解决这个问题。 感谢。

1 个答案:

答案 0 :(得分:1)

根据您的库,您必须将文件名传递给Csv_impot构造函数。但是你正在传递文件Handler。

所以改变你的代码如下。

//general oops method:
$csv=new csv_import($csv_dosen,",","dosen");

//In CI,
 $this->load->library('csv_import',array($csv_dosen,",","dosen")); // no need to create object again. Array of values will be parameter for constructor.