I'm trying to create a feature in my Content Management System in which users can upload a CSV file which is then parsed and the data is put in a MySQL database. To do this I use a file input and this SQL query.
$sql = "LOAD DATA LOCAL INFILE '".$_FILES["file"]["tmp_name"]."'
INTO TABLE persons
FIELDS TERMINATED BY ';'
LINES TERMINATED BY '\r'
(id, name, email, contacts)";
This works perfectly for a .csv
file I created on my computer, but not al CSV files have fields that are terminated by a semicolon and have lines that are terminated with a \r
. Now I want this query to work for all .csv
files. Otherwise this feature won't work well enough to be implemented in my CMS. Is there any way I can make this work for .csv
files which have other field and line endings? (e.g \n, \r\n, ,)