PHP не редактирует текстовый файл? - PullRequest
0 голосов
/ 12 февраля 2019

У меня есть этот кусочек PHP-кода, который ранее работал нормально при открытии и редактировании .txt файла, однако код больше не работает.Может кто-нибудь объяснить, что происходит и решение, если это возможно?Этот код должен позволять пользователю вводить имя и имя, которое пользователь хочет заменить в текстовом файле, однако этого не происходит, код работает нормально, однако он не редактирует документ.

<form method="post" 
      name="change_name and surname"
      action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> <!--call the php within this page-->

        Name:
        <input name="Name" type="text" id="sname" />
        <br /><br />            

        Names to Replace:
        <input name="Names" type="text" id="sname" />
        <br /><br />    

        Surname:
        <input name="Surname" type="text" id="name" />
        <br /><br />

        Surnames to Replace:
        <input name="Surnames" type="text" id="name" />
        <br /><br />    

      <input type="submit" value="Submitt" />


</form> 


<?PHP
//must have input form to get the data from the user
if (!empty($_POST['Name'])&& !empty($_POST['Surname'])&&($_POST['Names'])&& !empty($_POST['Surnames']))

    {
        //store value to the local variable
        $Name = $_POST['Name'];// original name which is has been added to the add2.php file//
        $Names = $_POST['Names'];// replacement for the name//
        $Surname = $_POST['Surname'];
        $Surnames = $_POST['Surnames'];

        $fullname = $Name. " " .$Surname ;
        $fullname2 = $Names. " " .$Surnames ;
        $file = "names.txt";// manufacture1.txt is the file where it has been used manipulate the name and surname replacement//

        $haystack =  file($file);// manipulates the data using  "a" append as saved the name and surname in first step and changed using t 

        for($i=0; $i < count($haystack); $i++)//$i < count means i is greater than count//

        {
                $haystack[$i] = str_ireplace($fullname, $fullname2, $haystack[$i]); // haystack has been to used to loop//
        }       

        file_put_contents($file, $haystack);
    }

else{
    echo"<b>Please input Name and Surname !!</b>";// will ask the user to input their name and surname//
}


?>
...