user8409158
user8409158

Reputation:

how to create a static page in local directory from php form

I want to create a php form page. Which when submitted then it automatically create a html static page with filled form data in local directory of the web server. tell me please how can i do that.

Upvotes: 0

Views: 477

Answers (2)

Sanmeet
Sanmeet

Reputation: 1410

Since it us tough to write html , meta tag etc every time in textarea ....

So creating a template file and then using it as base , and finally editing only the body and title tag ( you can do more ) , it makes life much easier ...

First we need to create file.php which harnesses all the logic of creating a new file and injecting all the code ( using php methods fopen which creates a new file and file_put_contents which puts all the data inside it ).

Secondly , we create a template file from which all the boilerplate is used up. (Containing some variable like {BODY} ).

finally we use file.php by filling form and get all required inputs ( getting all inputs and using loop to change all the template variables with inputs entered ).

file.php

   <?php
    if ($_SERVER["REQUEST_METHOD"] === "POST") {
      if (isset($_POST['filename'])) {
        $body = $_POST['body'];
        $title = $_POST['title'];
        $swap_var = array(
          "{BODY}" => $body,
          "{TITLE}" => $title
        );
        $template = "template.php";
        if (file_exists($template)) {
          $html = file_get_contents($template);
        } else {
          die ("Unable to locate your template file");
        }
        foreach (array_keys($swap_var) as $key) {
          if (strlen($key) > 2 && trim($swap_var[$key]) != '')
    
            $html = str_replace($key, $swap_var[$key], $html);
        }
         $filename = $_POST['filename'];
          if (!file_exists($filename)) {
            fopen($filename, "w");
          }
         file_put_contents($filename , $html);
      }
    }
    ?>
    
    
    <!DOCTYPE html>
    <html>
    <head>
      <meta http-equiv="content-type" content="text/html; charset=utf-8" />
      <meta name="viewport" content="width=device-width">
      <title>Creating and adding text in File</title>
      <style type="text/css" media="all">
        input , textarea {
          width: 100% ;
        }
        body{
          padding: 10%;
        }
      </style>
    </head>
    <body>
      <form action="file.php" method="post" accept-charset="utf-8">
        <input type="text" name="filename" id="filename" placeholder="Enter Filename (with extension like .html)" />
        <br>
        <br>
        <input type="text" name="title" id="title" placeholder="title" />
        <br>
        <br>
        <textarea name="body" id="body" rows="8" placeholder="enter body"></textarea>
        <br>
        <br>
        <button type="submit">Create New html file</button>
      </form>
    </body>
    </html>

template.php

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width , initial-scale=1.0">
    <title>{TITLE}</title>
  </head>
  <body>
    {BODY}
  </body>
</html>

Make sure to create these 2 files in same directory and note this code will generate all the new files in the same directory as these 2 files , remember you can always change the location of generation of new files .

Upvotes: 2

Pranav Ghodake
Pranav Ghodake

Reputation: 81

You can use php file functions like example.

$myfile = fopen("WHATEVER_THE_FILENAME_YOU_WANT.html", "w") or die("Unable to open file!");
$data = """
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>

ECHO YOUR FORM DATA HERE.

</body>
</html>
""";
fwrite($myfile, $data);
fclose($myfile);

Upvotes: 0

Related Questions