Automatically import CSV file and upload to database

One of my clients has all the information about his product processed by an external source. They provided me with this in a CSV file, which they will regularly update and upload to the ftp folder of my specification, say, every week.

Inside this CSV file is all the product information; product name, specification, image location, etc.

The website that I created for my client has a MySQL database, which I thought would contain all the product information and, thus, was created to process all the product data.

My question is this: how do I start creating and running a script that will find the newly added CSV file from the specified FTP folder, extract the data and replace all the data in the corresponding MySQL table, is everything done automatically?

Is it possible?

Any help would be greatly appreciated since I do not want to use the IFrame, S parameter.

+5
source share
4 answers

You can simply make a script that reads the csv file using the fread php function, extracts each line and formats it into an array to insert it into the database.

$fileTemp = "path-of-the-file.csv";
$fp = fopen($fileTemp,'r');
$datas = array()
while (($data = fgetcsv($fp)) !== FALSE)
{
     $data['productName'] = trim($data[0]);
     $data['spec'] = trim($data[1]);
     $data['imageLocation'] = trim($data[2]);
     $datas[] = $data;
}

Now you have prepared an array $datasthat you can insert into the database with iterations.

+5
source

fgetcsv? , cron .

0

, , :

  • mtime - (, , )
  • script, X cron

script mtime csv . mtime , SQL-, :

LOAD DATA LOCAL INFILE '/var/www/tmp/file.csv' REPLACE INTO TABLE mytable COLUMNS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\r\n'

touch , . scv mtime greate, "" , .

LOAD DATA INFILE SQL

, , , ( , mtime).

0
source

All Articles