require_once "classes/connection.php";
//open mysql database connection
$mysqli = openConnection();
//handles the upload of the CSV file
if (isset($_POST['submit'])) {
    $fname = $_FILES['fileToUpload']['name'];
    //echo 'Upload file name: '.$fname.' ';
    $chk_ext = explode(".", $fname);
    if (strtolower(end($chk_ext)) == "csv") {
        $filename = $_FILES['fileToUpload']['tmp_name'];
        //echo 'Upload file name (b): '.$filename.' ';
        $handle = fopen($filename, "r");
        //boolean used to track success of upload, returns true if all records uploaded successfully, false otherwise
        $successBool = true;
        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
            $successBool = $successBool && uploadContactsInfo($mysqli, $data);
        }
        fclose($handle);
        if ($successBool) {
            //we notify user of successful upload of CSV file
            createNotif("success", $fname . ' successfully imported');
        } else {
            //we notify user of unsuccessful upload
            createNotif("warning", "Upload unsuccessful. Please see error messages");
        }
    } else {
        createNotif("warning", "Invalid filetype. Please only upload CSV files");
    }
}
// close connection
closeConnection($mysqli);
Exemple #2
0
<?php

// include the configs / constants for the database connection
require_once "config/db.php";
//open mysql database connection
$mysqli = openConnection();
//display contacts information for the logged in user in table rows
if (isset($_POST['submit'])) {
    $fname = $_FILES['fileToUpload']['name'];
    echo 'upload file name: ' . $fname . ' ';
    $chk_ext = explode(".", $fname);
    if (strtolower(end($chk_ext)) == "csv") {
        $filename = $_FILES['fileToUpload']['tmp_name'];
        $handle = fopen($filename, "r");
        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
            uploadContactsInfo($mysqli, $data);
        }
        fclose($handle);
        echo "Successfully Imported";
    } else {
        echo "Invalid filetype. Please only upload CSV files";
    }
}
// close connection
closeConnection($mysqli);