【问题标题】:Overwrite a single value in a text file using php使用php覆盖文本文件中的单个值
【发布时间】:2022-01-25 21:06:23
【问题描述】:

我正在尝试编写一个代码,该代码应该为一个帐户存储一定天数。然后,该帐户的用户应该能够使用 html 选择特定的时间范围。然后应该从总金额中减去这些天数,并将新值放回文本文件中。

到目前为止,我已经尝试通过将 txt 文件读入数组并从那里工作来解决很多问题,但这就是我遇到死胡同的地方,因为我无法找到解决问题的方法.

我现在的工作方式是这样的:

if(isset($_POST['submit_btn'])) //If the submit button is pressed, it starts working through the steps
                {
                    $start1 = date_create($_POST["start"]); //I convert the HTML dates to php dates so i can work with them in a second
                    $end1 = date_create($_POST["end"]);
                    $start2 = date_format($start1, "Y-m-d");
                    $end2 = date_format($end1, "Y-m-d");
                    $end = $end2;
                    $start = $start2;
                    $d_diff1 = date_diff(date_create($end), date_create($start)); //I create the date difference and convert them back into a value I can work with
                    $d_diff = $d_diff1->format('%d days');
                    $up = fopen('text\urlaub.txt', 'a+');

                    if($d_diff < 0) //The next few lines are technically irrelevant for the problem
                    {
                        echo "Ihr Startdatum muss vor dem Enddatum liegen.";
                    }
                    elseif($d_diff == 0)
                    {
                        echo "Sie können heute keinen Urlaub mehr legen.";
                    }
                    elseif($d_diff >= 35)
                    {
                        echo "Ihr Antrag wurde gestellt. Aufgrund der Länge dea Beantragten Urlaubs muss dieser erst manuell bestätigt werden. Sie werden innerhalb von 2 Werktagen benachrichtigt.<br>";
                    }
                    elseif($d_diff >= 1 && $d_diff <= 35) //Here is where the problem starts
                    {
                        echo "Ihr Antrag wurde eingereicht und von System freigegeben. Sollte es Probleme mit ihrem Antrag geben wird sich in den nächsten 2 Tagen ein Mitarbeiter an sie wenden. <br>";
                        $urlaubkey = str_word_count(file_get_contents('text\urlaub.txt'),1,'üöä1234567890-.:;_<>|@€!§%&/=?'); //I read the txt file into the array
                    }
                    $key = array_search($name, $urlaubkey); //I check for the username in the array. If I find it, the array key for the username is saved as a variable
                    echo $key; //Next step would be to increase the value of this by 1 to get the correct key. Then i'd have to overwrite the txt file.

这基本上就是我现在所处的位置。唯一缺少的其他事情是我将数组键加一并运行简单的减法,但这不是真正的问题

我的txt文件保存值的方式是这样的:

körner,26
werner,26
albert,30
wernher,26
Walther,34

它遵循'用户名','剩余天数'的方案

任何帮助将不胜感激

【问题讨论】:

  • 是否需要遵循文件中数据的给定格式?或者是否也可以使用另一种数据存储格式,例如 JSON?
  • 如果我可以选择保留为 txt。

标签: php


【解决方案1】:

我建议您使用serializeunserialize 函数对包含数据的数组内容进行序列化和反序列化;这将使您更容易操作数组的内容

请看下面的示例代码

<?php

define("FILENAME", "C:\TEMP\UserList.txt");

// Create an array containing a list of users 

$userList = array(
    'USER0001' => array(
        'name' => 'Fabio',
        'number' => 23,
    ),
    'USER0002' => array(
        'name' => 'Laura',
        'number' => 7,
    ),
);

// Serialize the array and write the contents to a file 

file_put_contents(FILENAME, serialize($userList));

// Read the contents from the file and deserialize the array 

$userList = unserialize(file_get_contents(FILENAME));

// Update the value of the 'number' field, identifying the user by its unique key 

$userList['USER0001']['number'] = 75;

// Write once more 

file_put_contents(FILENAME, serialize($userList));

// Read once more 

$userList = unserialize(file_get_contents(FILENAME));

// Dump the result 

echo '<pre>';
var_dump($userList);
echo '</pre>';

【讨论】:

  • 虽然序列化可能在这里工作,但我不能不把像PHP Objection Injection 这样的序列化带来的安全问题的风险。使用 JSON 这样的数据存储格式可以更好地分离数据和代码并降低安全风险。
  • @TheFRedFox,我不能不同意!但是,问题表明数据当前作为数组存储在文件中,因此我建议了最简单和最快的解决方案。尽管有许多优点,但将数组的内容序列化和反序列化为 JSON 需要对架构进行大量修改
  • 我明天会实现这个(不得不在周末把我的笔记本电脑送去维修),我会看看能不能让它工作。安全性也不重要,因为这仅适用于在本地主机上运行的学校项目
猜你喜欢
  • 1970-01-01
  • 2014-11-22
  • 2015-05-24
  • 1970-01-01
  • 2020-12-29
  • 2010-09-19
  • 2013-05-11
  • 2014-10-22
  • 2016-05-25
相关资源
最近更新 更多