/    Sign up×
Community /Pin to ProfileBookmark

html file creation

Hello,
I’m trying to be able to make html files with a simple form. I’d like a textbox for the file name, and then a textarea for the html code. Then it would just make a new html file with that filename and the code that was in the textarea in the <body> tag of the new file (the <head> and everything else besides the body tag would be preset.) I’m not really sure which function to use, fopen, fwrite, something else? The documentation of the functions doesn’t really discuss [I]creating[/I] files, just editing them. Any help is appreciated. Thanks!

-Steve

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@mitconMar 16.2005 — Steve:

Collect the file name and HTML code then on submit load the PHP file to do the transfer (could be the same script or a different one). The script to do the download should have the following logic:

1: Check to see if the file exists. If it does alert the user and ask for confirmation before overwriting. If the file does not exist continue on to #2, or the user agrees, go on to #2.

2: Open the file using fopen() and the correct mode. Write some hard coded "stuff". Write the entered "stuff". Write the final hard coded "stuff"

The PHP might look something like this:

[code=php]if (file_exists($fileName){
//alert the user before continuing
} else {
if ($fileHandle = fopen($fileName,"w+")) {
fwrite($fileHandle, $htmlPrelude); //everything including <body> tag
fwrite($fileHandle, $htmlCode); //continue doing fwrites until all of body lines are written
fwrite($fileHandle, $filePostlude); //could be only the </body> and </html> tags
} else {
//handle write error
} //end if fopen()
} //end if file_exists()
fclose($fileHandle);
[/code]


The fopen with the "w+" mode opens a file for read/write access and (THIS IS IMPORTANT) truncates the file to 0 length. This is why you must check for the existance of the the file BEFORE opening it.

Of course you would want to verify that the writes wet off without a hitch. You do that by checking the status returned from the fwrite(...) command.

Check out the PHP manual at [URL=http://www.php.net]http://www.php.net[/URL]. Search the Function Reference for [b]fopen(), fwrite(), file_exists() and fclose()[/b] for explanations and good examples of how to use these functions.
Copy linkTweet thisAlerts:
@balloonbuffoonauthorMar 16.2005 — Thanks for the reply and the code! I'll try it out as soon as I get a chance. Thanks.

-Steve
×

Success!

Help @balloonbuffoon spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...