自动导入CSV文件并上传到数据库 [英] Automatically import CSV file and upload to database

查看:1644
本文介绍了自动导入CSV文件并上传到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的一个客户的所有产品信息都由外部来源处理。他们已经在CSV文件中提供了这个文件,他们将会定期更新并上传到我的规范的ftp文件夹,例如每周。

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.

我为我的客户端构建的网站正在运行MySQL数据库,我认为这将是所有的产品信息,因此已经建立了处理所有的产品数据。

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.

我的问题是这样:我将如何创建和运行一个脚本,将找到一个新添加CSV文件从指定的FTP文件夹,提取数据,并替换相关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?

这是否甚至可能?

任何帮助将非常感谢,因为我不想使用iframe option,S.

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

推荐答案

你可以创建一个脚本,使用fp函数读取csv文件,格式在数组中插入数据库。

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;
}

现在您已准备好数组 $ datas 你可以插入到数据库与迭代。

Now you have prepared array $datas which you can insert into database with iterations.

这篇关于自动导入CSV文件并上传到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆