.html file creating

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
nix
Forum Newbie
Posts: 3
Joined: Fri Jun 18, 2004 8:42 am
Location: nixopax.com

.html file creating

Post by nix »

I am a n00b to PHP, not to HTML though. (First time ever trying PHP). I have a PHP page that will allow the user to input their name and then submit it. like a guestbook. but instead what I want it to do is either create a .txt file or .html file that always has a different name with their info.

alright:

user #1 types his name in the text box, presses submit and then the code creates 1.html with his info.

I assume that I will need some sort of permanent counter in it so that it will keep going to create #.html to accomodate for the number of guests that go there.

any help is welcome. including a slap back to reality. :mrgreen:

edit: one thing that I've gathered from other searches is that I might need MySql
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

No need for MySQL.

Here's what I'd do.
Firstly, create two variables, $header and $footer, which contain the HTML code that you want to appear above and below the users info, which we'll assume is in a variable called $info.

Then we'll create a temporary file with tmpfile(), and put all the data into it:

Code: Select all

<?php
$temp = tmpfile(); 
fwrite($temp, $header.$info.$footer);
?>
Now, let's create the html file - we need to see what number we need, so we'll make a loop that checks for $num.html and breaks when there isn't one:

Code: Select all

<?php
$num = 1;
while (file_exists($num.".html")) {
$num ++;
}
?>
Now we have our number, lets copy the temp file into a html file, and close the temp file (which will also delete it):

Code: Select all

<?php
copy ($temp, $num.".html");
close ($temp);
?>
To finish, let's redirect the user to the new page:

Code: Select all

<html>
<head>
<!-- Head Stuff -->
</head>
<body>
<a href="<?php
print $num.".html";
?>">Look at the new file</a>
</body>
</html>
All of the above functions (like copy or tmpfile) can be found in the PHP manual.

Good luck!
Post Reply