PHP无法编辑文本文件?

时间:2019-02-12 20:46:30

标签: php file editing

ive在这里得到了这段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//
}


?>

0 个答案:

没有答案