0

I want to import csv into mysql using codeigniter. This is my source code.

view

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

library

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

    }

controller

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

models

<?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/>"
        }
    }
}
?>

when I run this code, an erro display that [function.fopen]: failed to open stream: No such file or directory. How I solved this promblem? I hope you can help to solve this problem. Thanks.

10
  • what is this ".$handle."? Commented Feb 8, 2014 at 7:22
  • $handle is a variable that to catch of location of the csv file in my directory. Commented Feb 8, 2014 at 7:26
  • what is the need for . . here? Commented Feb 8, 2014 at 7:27
  • oh, I'm sorry ".$handle." must be $handle :) Commented Feb 8, 2014 at 7:32
  • no, an erro display that [function.fopen]: failed to open stream: No such file or directory. Commented Feb 8, 2014 at 7:36

1 Answer 1

1

As per your library, you have to pass file name to your Csv_impot constructor. But you are passing file Handler.

so change your code as below.

//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.
Sign up to request clarification or add additional context in comments.

1 Comment

an error display that Non-existent class: Csv_import

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.