I want to try using LOAD_DATA_INFILE, as I have search, its the easiest and fastest way to import csv data to my database table. I just have some questions I do hope anyone could tell me how.
I have files that were uploaded using an android device as my client. Now, I don't know the filenames, I just know that their file extension is ".csv". These are my issues:
I am trying to get the filenames using the glob() function, problem is, I also need the filename as it is one of the fields in my table. How can I do that? And with this case, is it possible for me to still use the LOAD_DATA_INFILE?
My current code can insert data from the csv file to my db table, but I want another workaround which is the load_data_infile. And I also cannot get the filename to be inserted.
$dir = "CsvFiles/";
$currentFile = glob($dir."*.csv");
foreach ( $currentFile as $filename) {
$handle = fopen($filename,"r");
}
//loop through the csv file and insert into database
do {
$file = basename ($filename,".csv");
if ($data[0]) {
mysql_query("INSERT INTO tblCoordinates(filename,lat,lon,dtime,cmt,cnt,ea,area,bsn,hsn,husn,name) VALUES
(
'$file', //this should be the filename, I still cannot get it
'".addslashes($data[0])."',
'".addslashes($data[1])."',
'".addslashes($data[2])."',
'".addslashes($data[3])."',
'".addslashes($data[4])."',
'".addslashes($data[5])."',
'".addslashes($data[6])."',
'".addslashes($data[7])."',
'".addslashes($data[8])."',
'".addslashes($data[9])."',
'".addslashes($data[10])."'
)
");
}
} while ($data = fgetcsv($handle,1000,",","'"));
Note: I am still using mysql. my boss told me to use that instead of mysqli.