I want to let users upload their contacts from a .csv file (exported from various contact management programs) to the server and then save it in mysql. From what I've read on SO and elsewhere, MYSQL can import the data using LOAD DATA INFILE. It also seems like there is a LOCAL option to get it from the client side. My three-part question is: Do I need to use file upload to get the file to the server or can I directly access it via the LOCAL option of LOAD DATA INFILE? Since the application will service multiple users with their contacts, should I put all the contacts in one table? Since different users will have different structures in their .csv files what is the best approach to handling discrepancies in fieldnames/schema in the different .csv files?
Tentative code:
<html>
<body>
<form action="getcsvile.php" method="post"
enctype="multipart/form-data">
<input type="file" size=12 name="file" id="file" /><input type="submit" name="submit" value="Upload CSV File" /></form>
<body>
</html>
<?php
$userid = $_SESSION['userid'];
//error checking to make sure .csv file and other requirements met...then
//get extension, set to $ext
$target = "csvfiles/".$userid.".".$ext;
move_uploaded_file($_FILES["file"]["tmp_name"],$target);
//NEED TO PARSE DATA USING fgetcsv or something to examine, clean up csv data?
$sql = "LOAD DATA LOCAL INFILE '$target' INTO TABLE contacts FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n' IGNORE 1 LINES";// IS LOCAL right?
MySQL_query($sql);
$sql2 = "UPDATE contacts SET userid = '$userid';
MySQL_query($sql2);
echo "success;
Does this seem like the right approach? How hard is it likely to be to accomodate different csv files from different users?
Many thanks.