简体   繁体   English

自动导入CSV文件并上传到数据库

[英]Automatically import CSV file and upload to database

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. 他们已经在CSV文件中向我提供了这个文件,他们会定期更新并上传到我的规范的ftp文件夹,比如说每周。

Within this CSV file is all of the product information; 在此CSV文件中包含所有产品信息; 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. 我为我的客户构建的站点正在运行一个MySQL数据库,我认为该数据库将保存所有产品信息,因此可以构建来处理所有产品数据。

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? 我的问题是:我将如何创建和运行一个脚本,该脚本将从指定的FTP文件夹中找到新添加的CSV文件,提取数据,并替换相关MySQL表中的所有数据,这些都是自动完成的?

Is this even possbile? 这甚至可能吗?

Any help would be greatly appreciated as I don't want to use the IFrame option, S. 任何帮助将不胜感激,因为我不想使用IFrame选项,S。

You can just make a script which reads csv file using fread function of php, extract each row and format in an array to insert it into database. 您可以使用php的fread函数创建一个读取csv文件的脚本,提取数组中的每一行和格式以将其插入到数据库中。

$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 array $datas which you can insert into database with iterations. 现在您已准备好数组$datas ,您可以通过迭代将其插入到数据库中。

should be pretty straight forward depending on the csv file 应该非常简单,具体取决于csv文件

some csv files have quotes around text "", some don't some have , comma inside the quoted field etc 一些csv文件在文本“”周围有引号,有些没有,引号字段内有逗号等

depending on you level of php skills this should be reasonably easy 根据你的PHP技能水平,这应该是相当容易的

you can get a modified timestamp from the file to see if it is new 您可以从文件中获取修改后的时间戳,以查看它是否是新的

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

open the file and import the data 打开文件并导入数据

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

insert into the database 插入数据库

http://nz.php.net/manual/en/function.mysql-query.php 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 如果使用fgetcsv难以解析CSV,您可以尝试像PHPExcel项目那样具有csv读取功能的东西

http://phpexcel.codeplex.com http://phpexcel.codeplex.com

have you had a look at fgetcsv ? 你看过fgetcsv了吗? You will probably have to set up a cron job to check for a new file at regular intervals. 您可能需要设置一个cron作业来定期检查新文件。

All you need is: 所有你需要的是:

  • Store last file's mtime somewhere (let's say, for simplicity, in another file) 将最后一个文件的mtime存储在某处(比如说,为简单起见,在另一个文件中)
  • script that runs every X minutes by cron 由cron每隔X分钟运行一次的脚本

In this script you simply mtime of the csv file with stored value. 在这个脚本中,您只需使用存储值的csv文件的mtime。 If mtime differs, you run SQL query that looks like this: 如果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'

Optionally, you can just touch your file to know when you've performed last data load. (可选)您只需触摸文件即可知道何时执行了上次数据加载。 If the scv's file mtime is greate than your "helper" file, you should touch it and perform the query. 如果scv的文件mtime比你的“helper”文件大,那么你应该触摸它并执行查询。

Documentation on LOAD DATA INFILE SQL statement is here 关于LOAD DATA INFILE SQL语句的文档在这里

Of course there is a room for queries errors, but I hope you will handle it (you just need to be sure data loaded properly and only in this case touch file or write new mtime). 当然有一个查询错误的空间,但我希望你能处理它(你只需要确保数据正确加载,只有在这种情况下触摸文件或写新的mtime)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM