Import CSV File Data Into Database Using PHP - Mostlikers

10 July, 2014

Import CSV File Data Into Database Using PHP

Import CSV File Data Into Database Using PHP sometime large amount Database data not possibe to insert into at a time. Ex; E-commerce site add all product list.  that's why use to  CSV file import code . just create  Excel document enter your data save CSV format after that import to database.


Live Demo         Download

HTML

<form enctype="multipart/form-data" method="post"/>
    <label>Import csv file :</label>
        <input type="file" name="csv" id="csv" class="large"/>
    <input type="submit" name="submit" value="import file" />
</form>

PHP

<?php
    if(isset($_POST['submit']))
    {   
        if (isset( $_FILES['csv'] ))
        {
         $csv_file = $_FILES['csv']['tmp_name'];
            if ( ! is_file( $csv_file ) )
             exit('File not found.');     
              if (($handle = fopen( $csv_file, "r")) !== FALSE)
                {
                while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
                    {
                        echo $data[0];
                        echo $data[1];//csv file data
                        echo $data[2];
// write here your insert sql code
                    }
                fclose($handle);
                }
            echo "<script>alert('Data Uploaded successfully.');</script>";
            exit;
        }
        else
        {
        echo "<script>alert('Please select .CSV file.');</script>";
        exit;
        }
    }
?>

Update tutorial : Bulk Product Data Insert,Update CSV File into Database Using PHP and Mysqli








4 comments:

  1. I really works but all time it also inserted its heading..

    ReplyDelete
  2. Thanks Karthick, Works like a charm..

    ReplyDelete
  3. but how to export after factch data

    ReplyDelete