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

发布于 2024-11-01 02:52:11 字数 343 浏览 0 评论 0原文

我的一位客户的所有产品信息均由外部来源处理。他们以 CSV 文件形式向我提供了该文件,他们会定期更新该文件并将其上传到我指定的 ftp 文件夹中,比如每周一次。

此 CSV 文件包含所有产品信息;产品名称、规格、图像位置等。

我为客户构建的网站正在运行一个 MySQL 数据库,我认为该数据库将保存所有产品信息,因此已构建为处理所有产品数据。

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

这可能吗?

任何帮助将不胜感激,因为我不想使用 IFrame 选项,S.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

被你宠の有点坏 2024-11-08 02:52:11

应该非常简单,具体取决于 csv 文件,

有些 csv 文件在文本“”周围有引号,有些则没有
有些在引用字段内有逗号等,

具体取决于您的 php 技能水平,这应该相当容易,

您可以从文件中获取修改后的时间戳以查看它是否是新的

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

打开文件并导入数据

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

插入到数据库

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

如果 CSV 很难用 fgetcsv 解析
您可以尝试类似 PHPExcel 项目,它具有 csv 读取功能

http://phpexcel.codeplex.com

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

叶落知秋 2024-11-08 02:52:11

您可以制作一个使用 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;
}

现在您已经准备好了数组 $datas ,您可以通过迭代将其插入到数据库中。

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.

$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.

烛影斜 2024-11-08 02:52:11

您所需要的只是:

  • 将最后一个文件的 mtime 存储在某处(为了简单起见,在另一个文件中)
  • 由 cron 每 X 分钟运行的脚本

在此脚本中,您只需对具有存储值的 csv 文件进行 mtime 即可。如果 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 大于您的“帮助程序”文件,您应该触摸它并执行查询。

关于 LOAD DATA INFILE SQL 语句的文档在 这里

当然存在查询错误的空间,但我希望您能够处理它(您只需要确保数据正确加载,并且仅在这种情况下触摸文件或写入新的 mtime)。

All you need is:

  • Store last file's mtime somewhere (let's say, for simplicity, in another file)
  • script that runs every X minutes by cron

In this script you simply mtime of the csv file with stored value. If mtime differs, you run SQL query that looks like this:

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.

Documentation on LOAD DATA INFILE SQL statement is here

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).

丢了幸福的猪 2024-11-08 02:52:11

您看过 fgetcsv 吗?您可能需要设置一个 cron 作业来定期检查新文件。

have you had a look at fgetcsv? You will probably have to set up a cron job to check for a new file at regular intervals.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文