PHP/MYSQL Upload, import .csv file to mysql-process-table design

[亡魂溺海] 提交于 2020-01-06 01:54:19

问题


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.


回答1:


  1. Bear in mind the "client" from MySQL's perspective is the application which connects directly to it. In your case, that's PHP. The fact that PHP's "client" is the user's web-browser is irrelevant - and you almost certainly don't want your users connecting directly to the MySQL server.

    So, you might use LOCAL to pass the file from PHP to MySQL if they're running on different machines, but that'd be unnecessary if they're on the same machine and MySQL has access to read from wherever PHP has written the file.

  2. It really depends what you're going to do with the data, but on the whole - yes, put it all in one table.

  3. In the LOAD DATA statement you can tell MySQL which columns in the CSV map to which in the table - you could either make some guesses based on the file contents (especially if it includes a header row), or based on a hint provided by the user e.g. from which software the CSV originated; or, if you can't decide how the CSV maps to the table, you could present a short sample to the user in a browser and ask them to resolve it.




回答2:


Follow following steps to import csv contact file

Step 1. Upload the file using HTML form. Step 2. open the file using fopen.

Example $file=fopen($_FILES['file1']['tmp_name'],'r');

Step 3. Read the contents of file using fgetcsv. here you have to use while loop

Example: -

while($data = fgetcsv ($handle, 3000, ","))
{
       //write sql query to insert data in database
        $q="insert into contacts values($data[0],$data[1])";
}

If you want to allow multiple users to upload file with different structures. Create a column mapper. For example give them something like following

User File Fields          Mapping Fields (Database table fields)
Field 1                   Name                //Here you have to give a drop down menu
Field 2                   Phone Number        //Here you have to give a drop down menu
Field 3                   Email Address       //Here you have to give a drop down menu


来源:https://stackoverflow.com/questions/10657204/php-mysql-upload-import-csv-file-to-mysql-process-table-design

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!