Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically import CSV file and upload to database

Tags:

php

mysql

csv

One of my clients has all of his product information handled by an outside source. They have provided this to me in a CSV file which they will regulary update and upload to an ftp folder of my specification, say every week.

Within this CSV file is all of the product information; product name, spec, image location etc.

The site which I have built for my client is running a MySQL database, which I thought would be holding all of the product information, and thus has been built to handle all of the product data.

My question is this: How would I go about creating and running a script that would find a newly added CSV file from the specified FTP folder, extract the data, and replace all of the data within the relevant MySQL table, all done automatically?

Is this even possbile?

Any help would be greatly appreciated as I don't want to use the IFrame option, S.

like image 981
ss888 Avatar asked Apr 14 '11 11:04

ss888


1 Answers

should be pretty straight forward depending on the csv file

some csv files have quotes around text "", some don't some have , comma inside the quoted field etc

depending on you level of php skills this should be reasonably easy

you can get a modified timestamp from the file to see if it is new

http://nz.php.net/manual/en/function.lstat.php

open the file and import the data

http://php.net/manual/en/function.fgetcsv.php

insert into the database

http://nz.php.net/manual/en/function.mysql-query.php

If the CSV is difficult to parse with fgetcsv the you could try something like PHPExcel project which has csv reading capabilities

http://phpexcel.codeplex.com

like image 78
bumperbox Avatar answered Sep 20 '22 01:09

bumperbox