Page 1 of 1
Updating a html file with php... i'm stuck, please help
Posted: Thu Sep 12, 2002 3:48 pm
by mojo
Ok here's what i've been trying to accomplish. I want to call on a script somewhere on my website, in that script there is a textarea box with the code for a html file. I can then edit that code and click a submit button, which will then update the HTML file. I want this so i can easily update a page.
I've been asking around in other places and someone gave me a script to try, but i'm having trouble running that script. The script runs fine on my apache webserver on my Windows XP, but on my Linux computer, the file never updates, the code in the html file remains the same as before. This is either due to bad coding or file permissions, i honestly don't know. If someone could take a look at this, or even try this script on a webserver running Linux, i'd really appreciate it! (Or if you have a better script to accomplish this, let me know!)
Update.php
<?
$myHTMLFile = "test.html";
$myHTML = implode( file( $myHTMLFile ), "" );
if ( $save ) {
$tmpfile = fopen( $myHTMLFile, "w" );
$fp = fwrite( $tmpfile, $myHTMLText );
fclose($tmpfile);
}
?>
<html>
<head>
<title>Update.php</title>
</head>
<body>
<div align=center>
<form action="update.php">
<textarea name="myHTMLText" rows=35 cols=70><? echo $myHTML ?></textarea>
<br>
<input type="submit" name=save value=save>
</form>
</div>
</body>
</html>
Thanks, (sorry for long post)
Posted: Fri Sep 13, 2002 4:40 pm
by Takuma
You should update it from a different file say have form in form.php and process page in update.php and store data in data.txt or somthing...
Posted: Fri Sep 13, 2002 5:26 pm
by dusty
doesn't really matter if you use 1 file for the form and process code.
Code: Select all
<?
if(isset($_POSTїsave])) {
$data = "test.html";
$file = fopen("$data", "w");
$html = stripslashes($_POSTїhtml]);
fputs($file, $html);
fclose($file);
header("location: updated.html");
}
?>
<html>
<head>
<title>Update.php</title>
</head>
<body>
<div align=center>
<form action="<?=$PHP_SELF?>" method="post">
<textarea name="html" rows=35 cols=70><? require("test.html"); ?></textarea>
<br>
<input type="submit" name=save value=save>
</form>
</div>
</body>
</html>
Updating html file
Posted: Fri Sep 13, 2002 7:56 pm
by mojo
Ok, so i modified my code and ran the script only to see "Could not find 'updated.html'". Then i realized i needed to create that file... I didn't mention it in my first post, but that's exactly how i wanted things to work, update a file then go to a "your file's been updated page".
Even better... THE SCRIPT WORKED! Thank you! I've posted this topic 4 times in differnent forums, and the other script i got kinda worked but actually deleted the test.html file.
As an add on to this script... is there a way where i can make a drop down menu (or other kind of menu), select a html file, and then update it, using update.php? Or would it be best to create a update.php file for every html file that i'll be editing on the site?
Thanks for the help!
Posted: Fri Sep 13, 2002 11:19 pm
by dusty
sure it's possible.
Code: Select all
<?
$allow = array('example.html','example2.html','example3.html');
$data = "$_GETїfile]";
if(isset($_POSTїsave]) && in_array($data,$allow)) {
$file = fopen("$data", "w");
$html = stripslashes($_POSTїhtml]);
fputs($file, $html);
fclose($file);
header("location: updated.html");
}
?>
<html>
<head>
<title>Update.php</title>
</head>
<body>
<div align=center>
<form name="form" action="<?=$PHP_SELF?>" method="post">
<select name="file" onChange="location.href=document.form.file.value;">
<option>select one</option>
<option value="<?=$PHP_SELF?>?file=example.html">example</option>
<option value="<?=$PHP_SELF?>?file=example2.html">example2</option>
</select>
<textarea name="html" rows=35 cols=70><? require("$data"); ?></textarea>
<br>
<input type="submit" name=save value=save>
</form>
</div>
</body>
</html>
change && to && when you use it
Updating html file
Posted: Fri Sep 13, 2002 11:42 pm
by mojo
Hmmm... i thought the code was going to be much more complicated than that. I obviously have a lot to learn about PHP. I'll probably pick up a book on php, see what i can pick up, some of it seems pretty logical, and other parts of it are way out there (but hey, i gotta learn sometime). How'd you learn php? Can you recommend any books that were easy to understand/taught alot?
Thanks for all the help, i really really appreciate it!
Posted: Fri Sep 13, 2002 11:55 pm
by dusty
well i started with c so picking up php wasn't too hard. any questions i had usually could be answered by my friend if i couldn't figure it out from the manual. so i can't really refer you to any books, but i'm sure someone here knows of a good book to get.
Posted: Sat Sep 14, 2002 12:56 am
by Takuma
PHP Bible is quite good but very expensive... I recommend PHP Black Book.
All wrapped up
Posted: Sat Sep 14, 2002 5:34 pm
by mojo
Once again, thanks to all who have posted.
All the tips, info and code modifications have really really helped.
Thanks a ton!