Monday, 20 July 2015

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.

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];
                    }
                fclose($handle);
                }
            echo "<script>alert('Data Uploaded successfully.');</script>";
            exit;
        }
        else
        {
        echo "<script>alert('Please select .CSV file.');</script>";
        exit;
        }
    }
?>

0 comments:

Post a Comment