function createTemplate($sid, $dataSource_dir, $dataSource_dirPath, $excelFileMode, $ext)
{
    if ($ext == 'csv') {
        $template = 'csv-to-database.ktr';
    } else {
        $template = 'excel-to-database.ktr';
    }
    //'excel-to-target_schema.ktr';
    $newDir = "temp/{$sid}";
    if (!file_exists(mnmpath . $newDir)) {
        mkdir(mnmpath . $newDir);
    }
    if ($excelFileMode == 'append') {
        $ktrFilePath = mnmpath . "{$newDir}/{$sid}.ktr";
        $ktrFileURL = my_base_url . my_pligg_base . "/{$newDir}/{$sid}.ktr";
        if (!file_exists($ktrFilePath)) {
            copy($template, $ktrFilePath);
        }
        foreach (scandir($dataSource_dirPath) as $dataSource_filename) {
            if (FileUtil::isXLSXFile($dataSource_filename) || FileUtil::isXLSFile($dataSource_filename)) {
                $filenames[] = $dataSource_filename;
                $filePaths[] = $dataSource_dirPath . $dataSource_filename;
                $fileURLs[] = my_base_url . my_pligg_base . "/{$dataSource_dir}" . $dataSource_filename;
            }
        }
        $ktrManagers[$filenames[0]] = new KTRManager($template, $ktrFilePath, $filePaths, $sid, $fileURLs, $ktrFileURL, $ext);
    } else {
        foreach (scandir($dataSource_dirPath) as $dataSource_filename) {
            if (FileUtil::isXLSXFile($dataSource_filename) || FileUtil::isXLSFile($dataSource_filename) || FileUtil::isCSVFile($dataSource_filename)) {
                $filenames[] = $dataSource_filename;
                $filePath = $dataSource_dirPath . $dataSource_filename;
                $filePaths[] = $filePath;
                $fileURLs[] = my_base_url . my_pligg_base . "/{$dataSource_dir}" . $dataSource_filename;
                $ktrFilePath = mnmpath . "{$newDir}/{$dataSource_filename}.ktr";
                $ktrFileURL = my_base_url . my_pligg_base . "/{$newDir}/{$dataSource_filename}.ktr";
                if (!file_exists($ktrFilePath)) {
                    copy($template, $ktrFilePath);
                }
                $ktrManagers[$dataSource_filename] = new KTRManager($template, $ktrFilePath, array($filePath), $sid, $fileURLs, $ktrFileURL, $ext);
            }
        }
    }
    $_SESSION["ktrArguments_{$sid}"]["ktrManagers"] = serialize($ktrManagers);
    $_SESSION["ktrArguments_{$sid}"]["filenames"] = $filenames;
    $_SESSION["ktrArguments_{$sid}"]["filePaths"] = $filePaths;
    $_SESSION["ktrArguments_{$sid}"]["fileUrls"] = $fileURLs;
}
function upload_0($sid, $uploadTimestamp, $fileType, $excelFileMode, $dbType)
{
    $_SESSION["excelFileMode_{$sid}"] = $excelFileMode;
    // check file type
    $explodeRes = explode(".", $_FILES["upload_file"]["name"]);
    $extension = end($explodeRes);
    $mimes = array('xls', 'xlsx', 'csv', 'sql', 'zip');
    if (count($_FILES) <= 0) {
        $error = "ERROR: No file was uploaded.";
        $_SESSION["upload_file_{$sid}"]['error'] = $error;
    } else {
        if (!in_array($extension, $mimes)) {
            $error = "ERROR: please upload excel, csv, sql, or zip file.";
            $_SESSION["upload_file_{$sid}"]['error'] = $error;
        } else {
            //save upload file
            if ($_FILES['upload_file']['error'] > 0) {
                $error = "ERROR: " . $_FILES['upload_file']['error'] . "</br>";
                $_SESSION["upload_file_{$sid}"]['error'] = $_FILES['upload_file']['error'];
            } else {
                $upload_dir = mnmpath . "upload_raw_data/{$sid}/";
                if (!file_exists($upload_dir)) {
                    mkdir($upload_dir);
                }
                // Delete former files if user upload more than one time.
                if (isset($_SESSION["uploadTimestamp_{$sid}"]) && $_SESSION["uploadTimestamp_{$sid}"] != $uploadTimestamp) {
                    emptyDir($upload_dir);
                }
                $_SESSION["uploadTimestamp_{$sid}"] = $uploadTimestamp;
                // the file name that should be uploaded
                $file_tmp = $_FILES['upload_file']['tmp_name'];
                $raw_file_name = $_FILES['upload_file']['name'];
                $ext = pathinfo($raw_file_name, PATHINFO_EXTENSION);
                $_SESSION["fileExt_{$sid}"] = $ext;
                $upload_path = $upload_dir . $raw_file_name;
                // check upload status
                if (!move_uploaded_file($file_tmp, $upload_path)) {
                    $error = "ERROR: failed to save file.";
                    $_SESSION["upload_file_{$sid}"]['error'] = $_FILES['upload_file']['error'];
                } else {
                    // If a csv file is provided, create a excel file and write the csv value to it.
                    if (strtolower($ext) == 'csv') {
                        // $csvFilePath = $upload_path;
                        // $xlsxFilePath = FileUtil::convertCSVtoXLSX($csvFilePath);
                        // $xlsxFileName = pathinfo($xlsxFilePath, PATHINFO_BASENAME);
                        // $raw_file_name = $xlsxFileName;
                        //unlink($csvFilePath);
                    } else {
                        if (strtolower($ext) == 'zip') {
                            $_SESSION["excelFileMode_{$sid}"] = "append";
                            // If a zip file is provided, unzip all files.
                            $zipFilePath = $upload_path;
                            $zip = new ZipArchive();
                            $res = $zip->open($zipFilePath);
                            if ($res === TRUE) {
                                $zip->extractTo($upload_dir);
                                $zip->close();
                            } else {
                                $error = "ERROR: failed to unzip file.";
                                $_SESSION["upload_file_{$sid}"]['error'] = $error;
                            }
                            // Convert all csv files into xlsx files.
                            $dirFiles = scandir($upload_dir);
                            $i = 1;
                            foreach ($dirFiles as $filename) {
                                $filePath = $upload_dir . $filename;
                                if (FileUtil::isCSVFile($filePath)) {
                                    $_SESSION["fileExt_{$sid}"] = "csv";
                                    break;
                                    // $newFilePath = $upload_dir . $i++ . '.csv';
                                    // rename($filePath, $newFilePath);
                                    // $filePath = $newFilePath;
                                    // $csvFilePath = $filePath;
                                    // $xlsxFilePath = FileUtil::convertCSVtoXLSX($csvFilePath);
                                    // $xlsxFileName = pathinfo($xlsxFilePath, PATHINFO_BASENAME);
                                }
                                //unlink($filePath);
                            }
                        } else {
                            if (strtolower($ext) == 'sql') {
                                try {
                                    $dbImporter = DatabaseImporterFactory::createDatabaseImporter($dbType, $sid, my_pligg_base_no_slash);
                                    $dbImporter->importDbSchema($upload_path);
                                    // Store dump file path for the last step.
                                    $_SESSION["dump_file_{$sid}"] = $upload_path;
                                } catch (Exception $e) {
                                    $_SESSION["upload_file_{$sid}"]['error'] = $e->getMessage();
                                }
                            }
                        }
                    }
                    $loc_msg = "uploaded successfully";
                    $_SESSION["upload_file_{$sid}"]['loc'] = $loc_msg;
                }
            }
        }
    }
    $json_response = array();
    if (isset($_SESSION["upload_file_{$sid}"]['error'])) {
        $json_response['isSuccessful'] = false;
        $json_response['message'] = $_SESSION["upload_file_{$sid}"]['error'];
    } else {
        $json_response['isSuccessful'] = true;
        $json_response['message'] = $_SESSION["upload_file_{$sid}"]['loc'];
    }
    unset($_SESSION["upload_file_{$sid}"]['error']);
    unset($_SESSION["upload_file_{$sid}"]['loc']);
    echo json_encode($json_response);
}