<?php 
$host='localhost'; // Host Name.
$db_user= 'root'; //User Name
$db_password= 'password';
$db= 'ngipassbook'; // Database Name.
$conn=mysql_connect($host,$db_user,$db_password) or die (mysql_error());
mysql_select_db($db) or die (mysql_error());
mysql_query("SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'");
if (isset($_POST['importfile'])) {
    if (is_uploaded_file($_FILES['csv']['tmp_name'])) {
        //echo "<h1>" . "File ". $_FILES['csv']['name'] ." uploaded successfully." . "</h1>";
        //echo "<h2>Displaying contents:</h2>";
    }
    $count = 0;
    $handle = fopen($_FILES['csv']['tmp_name'], "r");
    function rmws($input){
        $input = preg_replace("/[^a-zA-Z0-9\-\&\,\>\<]+/", " ", $input);
        return $input;
    }
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $count++;
        
        if($data[0]!='' && $data[1]!=''){
            if($count>1){ 
                $categoryid = $data[0];
                $name=$data[1];
                $address=rmws($data[2]);
                $city=$data[3];
                $mobile=$data[4];
                $status='1';
                $is_delete='0';
                $create_datetime=date('Y-m-d h:i:s');
                
                $import="INSERT into contact(categoryid,name,address,city,mobile,status,is_delete,create_datetime) values('".ucwords(strtolower($categoryid))."','".ucwords(strtolower($name))."','".nl2br(ucwords(strtolower($address)))."','".addslashes($city)."','".addslashes($mobile)."','1','0','".trim($create_datetime)."')";
                
                $result = $db->query($import);
                
                $last_id = mysql_insert_id();
            }
        }
    }

    fclose($handle);
    
    echo "<script>window.location.href = 'fileimport.php?recordmsg=successadd';</script>";
    exit;
}
?> 
    <style type="text/css">.red{color: red;}</style>
    <!-- PAGE CONTENT WRAPPER -->
    <div class="page-content-wrap">
    
        <div class="row">
            <div class="col-md-12">
                <form class="form-horizontal" name="services-form" id="services-form" enctype="multipart/form-data" method="post">
                    <div class="panel panel-default">
                        <div class="panel-heading">
                            <h3 class="panel-title">
                                <strong>
                                File Import
                                </strong>
                            </h3>
                            
                        </div>
                        <div class="panel-body">
                            <div class="form-group">
                                <label class="col-md-3 col-xs-12 control-label">Select File <span>*</span></label>
                                <div class="col-md-6 col-xs-12"> 
                                    <div class="input-group"><span class="input-group-addon"><span class="fa fa-upload"></span></span>
                                        <input type="file" class="fileinput btn-success" name="csv" id="csv" title="Select file for import" onchange="filevalidateornot()" required="" />
                                        <!-- onchange="filevalidateornot()" -->
                                    </div>
                                </div>
                            </div>
                        </div>
                        
                        <div class="panel-footer"> 
                        	<input type="submit" class="btn btn-primary pull-right" value="Import" name="importfile">
                        </div>
                    </div>
                </form>
            </div>
        </div> 

    </div>
    <!-- END PAGE CONTENT WRAPPER -->                                            

<?php 
    $alertmsg= isset($_GET['recordmsg'])?trim($_GET['recordmsg']):'';
    if(isset($alertmsg)=='successadd' && $alertmsg !='successedit' &&  $alertmsg !='successdelete'  &&  $alertmsg !=''){
?>
<div class="message-box message-box-info animated fadeIn open" id="message-box-info">
    <div class="mb-container">
        <div class="mb-middle">
            <div class="mb-title"><span class="fa fa-info"></span> <strong>FILE IMPORT SUCCESSFULLY</strong></div>
            <div class="mb-content">
                <p>FILE IMPORT SUCCESSFULLY.</p>
            </div>
            <div class="mb-footer">
                <button class="btn btn-default btn-lg pull-right mb-control-close" onclick="window.location.href='fileimport.php'">Close</button>
            </div>
        </div>
    </div>
</div>
<?php
    }                      
?>
<script type="text/javascript">
    function filevalidateornot(){
        if( document.getElementById("csv").value.toLowerCase().lastIndexOf(".csv")==-1){
            alert("Please upload a file with .csv extension.");
            $('#csv').val('');
            return false;  
        }
    }
</script>
