如果不在php中重写整个文件,我该如何将文本(变量)放在文件顶部


How would I go about putting text (variable) on the top of a file without rewriting the whole file in php?

如果不使用PHP重写整个文件,如何将文本(变量)放在文件顶部?我有三个文件:index.phpform.phpprocess.php

form.php有一个表单,我可以在上面输入数据,它会通过POST将信息传递给process.php

process.php将从form.php获取信息,并在变量中运行信息。我将使用这些变量创建一个文件(index.php),然后将信息写入文件,该文件将是index.php

我想让我的脚本做到这一点:取表单信息并将信息存储在config.php上的变量中,然后一旦完成,它将在顶部的index.php上包含它,而不重写所有index.php

process.php:

`<html>
   <head>
      <title>configure</title>
   </head>
   <body>
      <?php 
         include('config.php');
         $user = $_POST["name"]; 
         $page_name = $_POST["page_name"];
         $page_title = $_POST["page_title"];
         $message = $_POST["message"];
         print("<h1>congratulations!</h1><br><h2>Your have created your page</h1><a href=$page_name.php>Here</a>");
         $out = fopen("$page_name.php", "a"); 
         if (!$out) { 
            print("Could not append to file"); 
            exit; 
         } 
         fputs ($out,implode,("'n")); 
         fwrite($out,"<html>'n");
         fwrite($out,"<head>'n");
         fwrite($out,"<title>$page_title</title>'n");
         fwrite($out,"</head>'n");
         fwrite($out,"<body>'n");
         fwrite($out,"<h1>$message</h1>'n");
         fwrite($out,"</body>'n");
         fwrite($out,"</html>'n");
         fclose($out);
      ?>
   </body>
</html>`

form.php:

<html>
   <head>
      <title>Configure</title>
   </head>
   <body>
      <form action="process.php" method="POST">
         Your Name:<br />
         <input type="text" name="name"><br />
         Page Name:<br />
         <input type="text" name="page_title"><br />
         Page Title:<br />
         <input type="text" name="page_name"><br />
         Your Message:<br />
         <textarea name="message"></textarea><br />
         <input type="submit" value="Send Info">
      </form>
   </body>
</html>

听起来你正试图将表单中的参数传递给process.php,然后再传递回index.php。你真的不必这样做。只需将数据发布到您所在的同一文件(index.php)中即可。这意味着,将表单编辑为此<form action="" method="POST">,并在index.php文件中进行验证。然后您就可以访问$_POST内容。