<?php
ini_set('mssql.charset', 'UTF-8');
$hostname = 'GMGIS\SQLEXPRESS';
$username = 'BIMuser';
$password = 'Component3444';
$db_name = "21000X";
$account = "maabim";

try {
    $pdo = new PDO('sqlsrv:Server=' . $hostname . ';Database=' . $db_name, $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    echo $e;
    echo json_encode('Error connecting to the server.');
    die();
}

$paths = array_diff(scandir("./csv"), array('.', '..', 'Thumbs.db'));

foreach ($paths as $filename) {
    echo $filename;
    $row = 1;
    if (($handle = fopen("./csv/".$filename, "r")) !== FALSE) {
        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
            $data = array_map("utf8_encode", $data); //added
            $num = count($data);

            if ($row > 1) {
                for ($c = 0; $c < $num; $c++) {
                    echo $data[$c] . "<br>";
                }
               $sth = $pdo->prepare("INSERT INTO [21000X].[dbo].[File_Table]
                   ([project_id]
                   ,[SN]
                   ,[type_id]
                   ,[original_name]
                   ,[filename])
             VALUES
                   ('00000',
                   '".$data[0]."',
                   '".explode(".",$filename)[0]."',
                   '".$data[1]."',
                   '".$data[2]."')");
        $sth->execute();
                echo "<br />\n";
            }
            $row++;
        }
        fclose($handle);
    }
}
