使用 PHP 检测文件中任何不需要的字符和行数

问题描述 投票:0回答:1

我有一个无法解决的问题。而且我无法得到任何反馈,因为我只能直接在服务器上运行脚本,而没有任何可能的调试。

我的文件通过 FTP 传输到服务器,我必须逐一读取它们以将它们放入 SQL Server 数据库中。

我的文件看起来像这样:

2024-02-22 19:50:44
17.90
50.40
0.0
0.0
0.0
0.0
0.0
4.17

如果我像这样运行我的 PHP 代码,没问题:

foreach ($listWeathStatV2 as $weathStat) {
    $fileToRead = "/data/DB_" . $weathStat . ".txt";
    $file = fopen(__DIR__ . $fileToRead, "r") or die("Unable to open the file!");
    rewind($file);
    while(!feof($file)) {
            $heure       = fgets($file);          // Store date value
            $temp        = fgets($file);          // Store temp value
            $humidity    = fgets($file);          // Store hum value
            $pressure    = fgets($file);          // Store pressure value
            $pluvio      = fgets($file);          // Store pluvio value
            $anemo       = fgets($file);          // Store anemo value
            $gir         = fgets($file);          // Store gir value
            $lumino      = fgets($file);          // Store lumino value
            $battLvl     = fgets($file);          // Store battery level value

            $query    = "INSERT INTO " . $weathStat . " (heure, valTemp, valHumid, valPressure, valPluvio, valAnemo, valGir, valLumino, battLevel) VALUES ('$heure', '$temp', '$humidity', '$pressure', '$pluvio', '$anemo', '$gir', '$lumino', '$battLvl')";
            $result   = mysqli_query($connect, $query) or die("Errant query:  ".$query);
        fclose($file);

        $file= fopen(__DIR__ . $fileToRead, "w");
        rewind($file);
        file_put_contents($fileToRead, "");
        fclose($file);
    }
}

问题是,有时,我的文件看起来像这样:

2024-02-22 19:55:27
27.19
0.0

0.0&#¤
0.0
0.0
4.07

2024-02-22 19:55:27
27.19
0.0
0.0
0.0
0.0
0.0
0.0

因此我修改了代码,将文件转换为字符串(使用 file_get_contents),并且仅在没有不需要的字符(使用 preg_match)或行数不正确时才发送数据:

foreach ($listWeathStatV2 as $weathStat) {
    $fileToRead = "/data/DB_" . $weathStat . ".txt";
    $file = fopen(__DIR__ . $fileToRead, "r") or die("Unable to open the file!");
    $fileToString = file_get_contents($file);
    if ($fileToString === false) {
        echo "An error occurred while reading the file.";
    }
    else if (fgets($file) != "" && !preg_match('/^£$%&*()}{@#~?><>,|=_+¬¤ÇÓü]/', $fileToString) && (substr_count($fileToString, "\n" ) == 8)) {
        rewind($file);
        while(!feof($file)) {
            $heure       = fgets($file);          // Store date value
            $temp        = fgets($file);          // Store temp value
            $humidity    = fgets($file);          // Store hum value
            $pressure    = fgets($file);          // Store pressure value
            $pluvio      = fgets($file);          // Store pluvio value
            $anemo       = fgets($file);          // Store anemo value
            $gir         = fgets($file);          // Store gir value
            $lumino      = fgets($file);          // Store lumino value
            $battLvl     = fgets($file);          // Store battery level value

            $query    = "INSERT INTO " . $weathStat . " (heure, valTemp, valHumid, valPressure, valPluvio, valAnemo, valGir, valLumino, battLevel) VALUES ('$heure', '$temp', '$humidity', '$pressure', '$pluvio', '$anemo', '$gir', '$lumino', '$battLvl')";
            $result   = mysqli_query($connect, $query) or die("Errant query:  ".$query);
        }
        fclose($file);

        $file= fopen(__DIR__ . $fileToRead, "w");
        rewind($file);
        file_put_contents($fileToRead, "");
        fclose($file);
    }
}

当我运行这段代码时,我唯一能告诉的是,如果文件正确,它可以发送数据,但如果有任何不需要的字符,它会阻止脚本的其余部分,然后,剩余的文件会出现并且不被处理。 有人可以告诉我这段代码有什么问题吗?

非常感谢您的帮助和建议。 洛朗

php preg-match
1个回答
0
投票

如果您使用

file_get_contents()
读取文件,则无需也使用
fgets()
读取它。

在下面的代码中我读过一次。我检查它是否包含除数字和空格之外的任何内容,如果发生这种情况,则跳过该文件。

然后我将文件分割成换行符处的行,丢弃任何空行。如果它包含正确的行数,我会将每一行分配给相应的变量。

然后我使用准备好的语句将变量替换到查询中,而不是直接变量插值。

foreach ($listWeathStatV2 as $weathStat) {
    $fileToRead = __DIR__ . "/data/DB_" . $weathStat . ".txt";
    $fileToString = file_get_contents($fileToRead);
    if ($fileToString === false) {
        echo "An error occurred while reading $weathStat.";
        continue;
    }
    if (preg_match('/[^-:.\s\d]/', $fileToString)) {
        echo "The file $weathStat contains invalid characters";
        continue;
    }

    $lines = array_filter(preg_split('/[\r\n]+/', $fileToString)); // Split file into lines
    if (count($lines) != 9) {
        echo "The file $weathStat doesn't have 9 lines.";
        continue;
    }
        
    [$heure, $temp, $humidity, $pressure, $pluvio, $anemo, $gir, $lumino, $battLvl] = $lines;
    $stmt = mysqli_prepare($connect, "INSERT INTO $weathStat (heure, valTemp, valHumid, valPressure, valPluvio, valAnemo, valGir, valLumino, battLevel) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)");
    myqli_stmt_bind_param($stmt, "sffffffff", $heure, $temp, $humidity, $pressure, $pluvio, $anemo, $gir, $lumino, $battLvl);
    mysqli_stmt_execute($query);
    file_put_contents($fileToRead, "");
}
© www.soinside.com 2019 - 2024. All rights reserved.