填充 MySQL 数据库读取文件夹中的文件 [英] Populating a MySQL db reading a file in a folder
问题描述
这是我的问题.在我的 FTP 服务器中,我有一个文件夹,其中包含一些由远程软件定期上传的 txt 文件(abc.txt、hallo.txt、123.txt 等等).这些 txt 文件包含 MySQL 数据库的数据.
Here is my problem. In my FTP server i have a folder with some txt files (abc.txt, hallo.txt, 123.txt ...et cetera), uploaded regularly by a remote software. These txt files contain data for a MySQL database.
我想知道:如何自动填充 MyQSL 数据库(例如,使用 cron 作业)?我已经有一个能够填充数据库的 php 文件,但是使用了POST"方法(不是自动的).我必须手动选择每个文件以从我的计算机上传和更新数据库.
I wonder: how can i populate the MyQSL database automatically (ex. using a cron job)? I already have a php file able to populate the database, but using a "POST" method (not authomatically). I have to select manually each file to upload and update the database from my computer.
我想设置一个 php 代码,用于读取和选取包含在我的 FTP 文件夹中的旧 .txt 文件,并提供更新数据库.我已经拥有能够读取 .txt 文件并推断数据以插入到数据库中的代码.
I'd like to setup a php code that reads and picks up the older .txt file, contained in my FTP folder, and provide to update the database. I already have code able to read inside an .txt file and extrapolate data to insert in the database.
预先感谢您的帮助.
垫子.
推荐答案
<?php
mysql_connect('localhost','root','root'); // MAMP defaults
mysql_select_db('yourdatabase');
$files = glob('*.txt');
foreach($files as $file){
mysql_query("LOAD DATA INFILE '".$file." INTO yourtablename");
}
?>
或在 Linux 上使用 bash 脚本
or using a bash script on Linux
#!/bin/bash
for f in *.csv
do
mysql -e "load data infile '"$f"' into table my_table" -u username --password=your_password my_database
done
您可以编辑 crontab 使脚本以指定的时间间隔运行.
You can edit crontab to make the script to run on specified intervals.
这篇关于填充 MySQL 数据库读取文件夹中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!