I want the following functionality using php
I have a csv file. Each file corresponds to a row in my database
There is a html form that will allow me to choose the csv file.
Then once the form is submitted, I must parse the csv file and insert data into the db accordingly
How do I go about doing this ?
Reading a CSV file can generally be done using the
fgetcsvfunction (depending on your kind of CSV file, you might have to specify the delimiter, separator, … as parameters)Which means that going through you file line by line would not be much harder than something like this :
(Not tested, but example given on the manual page of fgetcsv should help you get started)
Of course, you’ll have to get the correct path to the uploaded file — see the
$_FILEsuperglobal, and the section on Handling file uploads, for more informations about that.And, to save the data into your database, you’ll have to use the API which suits your DB engine — if using MySQL, you should use either :