php . .使用文本文件在分号表中插入(追加)一条记录

php... inserting (appending) a record into a semicolon table using a text file?

本文关键字:插入 追加 记录 一条 文本 文件 php      更新时间:2023-09-26

我正在使用一个文本文件,里面有一个分号表…我已经学会了如何获取我的数据。特别是抓取任何我需要的列…

<?php
$statfile=$_GET['scd'];
if ($statfile=='')
{
    $statfile="/et3mach/status.txt";
}
$temps = array();
$brdhover = array();
if (($handle = fopen($statfile, "r")) !== FALSE)
{
    while (($data = fgetcsv($handle, 1000, ";")) !== FALSE)
    {
        if(substr(trim($data[0]), 0, 3) == 'BRD')
        {
            $board = trim($data[0]);          // get the index (e.g "BRD 0") & strip leading or trailing blanks
            $temp =  trim($data[10]);         // the tempreture data is the 10th field; trim that also, to be safe.
            $temps[ $board ] = $temp;
            $brdhover[ $board ] = $data[11];  // Field 11 is the "hover" text; don't trim that for now...
        }
    }
    fclose($handle);
}
?>

但是插入更困难,所有我能想到的是插入一个记录到一个文本文件…

<?php
$companyfile = "data/company.txt";
if (isset($_POST['company']))
{
    $newData = nl2br(htmlspecialchars($_POST['company']));
    $handle = fopen($companyfile, "w");
    fwrite($handle, $newData);
    fclose($handle);
}
// ----------------------------
if (file_exists($companyfile)) {
    $companyData = file_get_contents($companyfile);
}

我如何结合这两个功能,以便我可以使用html表单插入和追加消息?在本例中,我有一个带有一列的分号表。

对于fopen()fwrite()fclose(),可以使用带有FILE_APPEND标志的包装函数file_put_contents,如下图所示

file_put_contents($file, $content, FILE_APPEND);