这是一个指导你的 php 脚本,你需要根据你的需要改变它,我已经在里面做了一些解释:
如果您想使用 ftp 执行此操作,您还需要建立一个 ftp 连接。
$db_connection = new mysqli("HOST","DB_USER","DB_PASS","DB"); // make mysqli connection to DB
$query = $db_connection->prepare("INSERT INTO yourtable (column1,column2) VALUES (?,?)");
$data = scandir('/PATH/TO/READ'); // Scan the dir your files are in
foreach ($data as $datas) {
if (0 === strpos($datas, 'logdata')) {
echo $datas."<br />"; //Show all the files that beginn with logdata
$file = 'PATH/TO/FILE/'.$datas;
unset($file[0]); //DELETE THE FIRST LINE IF FILE CONTAINS A HEADLINE ELSE DELETE THIS LINE
foreach ($file as $line)
{
$column = explode("\t", $line); // \t stands for tab-delimited, change it to what ever delimites your file (f.e. ; , . )
$example = $column[0]; // get content of first column
$example1 = $column[1]; // get content of second column
// .....
$query->bind_param('ss',$example, $example1)
$query->execute();
}}}
让我们调用文件 insertscript.php,那么你的 cronjob 将是:
0 */6 * * * nohup php /path/to/file/insertscript.php
nohup 将进程置于后台并为每次运行创建一个文件。如果您不想要这个文件,您可以将您的 cronjob 更改为:
0 */6 * * * nohup php /path/to/file/insertscript.php >/dev/null 2>&1