Updating a html file with php... i'm stuck, please help

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
mojo
Forum Newbie
Posts: 7
Joined: Thu Sep 12, 2002 3:48 pm

Updating a html file with php... i'm stuck, please help

Post 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)
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post 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...
dusty
Forum Contributor
Posts: 122
Joined: Sun Apr 28, 2002 9:52 pm
Location: Portsmouth, VA

Post by dusty »

doesn't really matter if you use 1 file for the form and process code.

Code: Select all

&lt;?
if(isset($_POST&#1111;save])) {
  $data = "test.html";
  $file = fopen("$data", "w");
  $html = stripslashes($_POST&#1111;html]);
  fputs($file, $html);
  fclose($file);
  header("location: updated.html");
}
?&gt;

&lt;html&gt; 
&lt;head&gt; 
&lt;title&gt;Update.php&lt;/title&gt; 
&lt;/head&gt; 
&lt;body&gt; 
&lt;div align=center&gt; 
&lt;form action="&lt;?=$PHP_SELF?&gt;" method="post"&gt; 
&lt;textarea name="html" rows=35 cols=70&gt;&lt;? require("test.html"); ?&gt;&lt;/textarea&gt; 
&lt;br&gt; 
&lt;input type="submit" name=save value=save&gt; 
&lt;/form&gt; 
&lt;/div&gt; 
&lt;/body&gt; 
&lt;/html&gt;
mojo
Forum Newbie
Posts: 7
Joined: Thu Sep 12, 2002 3:48 pm

Updating html file

Post 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!
dusty
Forum Contributor
Posts: 122
Joined: Sun Apr 28, 2002 9:52 pm
Location: Portsmouth, VA

Post by dusty »

sure it's possible.

Code: Select all

&lt;? 
$allow = array('example.html','example2.html','example3.html');
$data = "$_GET&#1111;file]"; 
if(isset($_POST&#1111;save]) &amp;&amp; in_array($data,$allow)) { 
  $file = fopen("$data", "w"); 
  $html = stripslashes($_POST&#1111;html]); 
  fputs($file, $html); 
  fclose($file); 
  header("location: updated.html"); 
} 
?&gt; 

&lt;html&gt; 
&lt;head&gt; 
&lt;title&gt;Update.php&lt;/title&gt; 
&lt;/head&gt; 
&lt;body&gt; 
&lt;div align=center&gt; 
&lt;form name="form" action="&lt;?=$PHP_SELF?&gt;" method="post"&gt; 
&lt;select name="file" onChange="location.href=document.form.file.value;"&gt;
  &lt;option&gt;select one&lt;/option&gt;
  &lt;option value="&lt;?=$PHP_SELF?&gt;?file=example.html"&gt;example&lt;/option&gt;
  &lt;option value="&lt;?=$PHP_SELF?&gt;?file=example2.html"&gt;example2&lt;/option&gt;
&lt;/select&gt;
&lt;textarea name="html" rows=35 cols=70&gt;&lt;? require("$data"); ?&gt;&lt;/textarea&gt; 
&lt;br&gt; 
&lt;input type="submit" name=save value=save&gt; 
&lt;/form&gt; 
&lt;/div&gt; 
&lt;/body&gt; 
&lt;/html&gt;
change && to && when you use it
mojo
Forum Newbie
Posts: 7
Joined: Thu Sep 12, 2002 3:48 pm

Updating html file

Post 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!
dusty
Forum Contributor
Posts: 122
Joined: Sun Apr 28, 2002 9:52 pm
Location: Portsmouth, VA

Post 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.
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

PHP Bible is quite good but very expensive... I recommend PHP Black Book.
mojo
Forum Newbie
Posts: 7
Joined: Thu Sep 12, 2002 3:48 pm

All wrapped up

Post by mojo »

Once again, thanks to all who have posted.
All the tips, info and code modifications have really really helped.

Thanks a ton!
Post Reply