2

Yes, i want to create an .html or .php (whatever) document with some forms.

Example:

The user opens a page (preference: php) and see 1 form for website title and 1 button. Ok, he put the website title of your preference an click in the button. This page send the information contained in the form and create an html/php document with the title that user put in form.

This is not about to show some variables in html with "echo", is about to really create some new document.

Anyone?

user2154508
  • 335
  • 2
  • 5
  • 12

4 Answers4

3

This should do what you want:

$title = $_POST['title'];
$html = '<html>
    <head>
        <title>' . htmlspecialchars($title) . '</title>
    </head>
    <body>Some HTML</body>
</html>';
file_put_contents('/path/to/file.html', $html);

That will create a file for you with the title the user submitted (assuming you are submitting by POST).

Styphon
  • 10,304
  • 9
  • 52
  • 86
  • I would add to this answer that OP should be aware of the destination folder permissions. http://stackoverflow.com/questions/20283626/why-does-file-put-contents-have-permission-issues-when-run-from-the-browser?rq=1 – Tomás Feb 24 '14 at 13:44
  • Hurray for XSS vulnerabilities! – Ja͢ck Feb 24 '14 at 14:03
  • Hurray for uploading a backdoor – Alma Do Feb 24 '14 at 14:07
  • @Jack There are [plenty](http://stackoverflow.com/questions/1996122/how-to-prevent-xss-with-html-php) of [other](http://stackoverflow.com/questions/4344703/xss-prevention-handling-script-is-enough) [questions](http://stackoverflow.com/questions/71328/what-are-the-best-practices-for-avoiding-xss-attacks-in-a-php-site) on SO that cover XSS. No need for me to repeat. Though I have updated my answer to at least prevent the basics. – Styphon Feb 24 '14 at 15:28
2

So your form will need to be something like

<form action="" method="post">
    <input type="text" name="pageTitle" id="pageTitle" />
    <input type="submit" name="formSubmit" value="Submit" />
</form>

Then you will need to following code to capture the value of the page title and create the html page

if($_POST['formSubmit']) {
    $newpage = '<html><head><title>' . $_POST['pageTitle'] . '</title></head><body><-- Whatever you want to put here --></body></html>';
    file_put_contents('newpage.html', $newpage );
}
Pattle
  • 5,983
  • 8
  • 33
  • 56
0

Try this :

<?php

$title = htmlspecialchars($_POST['title']);
$content = "<html><head><title>$title</title></head><body>Your content here</body></html>";

file_put_contents('index.html', $content);
ncrocfer
  • 2,542
  • 4
  • 33
  • 38
0

You would echo the contents as usual but you should send the following headers:

header('Content-Disposition: attachment; filename=file.html'); //or whatever name you like here
header('Content-Type: text/html'); //or text/php if the file is php

This will make the browser open a download dialoug for your file

Hazem Mohamed
  • 564
  • 3
  • 7