PDA

View Full Version : updating a html file with php... i'm stuck please help


mojo
09-13-2002, 12:27 AM
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)

firepages
09-13-2002, 02:15 AM
<?
$myHTMLFile = "test.html";

if ( $_POST['save'] ) {
$tmpfile = fopen( $myHTMLFile, 'w' );
$fp = fwrite( $tmpfile, $myHTMLText );
fclose($tmpfile);
}

if( is_file( $myHTMLFile ) ){
$myHTML = implode( '' , file( $myHTMLFile ) );
}
?>

<html>
<head>
<title>Update.php</title>
</head>
<body>
<div align=center>
<form method="POST" action="<?$_SERVER['PHP_SELF'];?>">
<textarea name="myHTMLText" rows=35 cols=70><? echo $myHTML ?></textarea>
<br>
<input type="submit" name="save" value="save">
</form>
</div>
</body>
</html>


test.html will have to have its permissions set correctly in order to write to it on *NIX

there was nothing in the code that was NT specific I just assume that your *NIX host has register_globals set off, which means that $_POST['save'] needs to be used instead of $save etc

+ you need to add method="POST" (or GET) to your form & your implode call was wrong.

mojo
09-13-2002, 02:46 AM
Great, thanks for the modifications in the code, i'll try that.

Now... what permissions should the file be set to? I don't have much experience with file permissions because i'm still learning about this stuff.

Any modifications that i need to make to the server/files is easy because my server is my other computer. It's not connected to the internet, it's just there for testing webpages/servers/etc... So i can easily change permissions or edit files.

Thanks for the help! I'm going to cross my fingers and see if it works.

downsize
09-13-2002, 07:19 AM
you will have to set the file's permissions to write in order to, well, write to the file.

chmod 666 test.html should do it. you can use an ftp program to run the chmod command, but it would be cooler if you did it with telnet (don't know what type of access you have to the linux server)

if you do have telnet, run a man on the command (man chmod) to get more information if you are curious to really know why you are using '666' :-}

mojo
09-14-2002, 01:49 AM
Ok, i chmodded test.html to 666, and i ran the script with the modifications... and it worked, well, kind of
The textarea showed the test.html file as it should, but when i made my changes, and clicked the save submit button... it kind of cleared the test.html file. So i guess the good news is there was progress, but it changed test.html in a bad way.

However... someone somewhere else showed me a different script, which is slightly different, and did work. Honestly, i don't know much about PHP so i can't tell you why theirs worked and this one didn't.

If anyone's curious to see that script, let me know and i'll post it.

And 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, using update.php? Would i need a seperate update.php for each file?

Thanks for all the help!

downsize
09-14-2002, 10:17 AM
And 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, using update.php? Would i need a seperate update.php for each file?

you can do just about anything with php :D

since you are not claiming to know php very well, this might not be that simple for you - but grab a good book and bookmark http://www.zend.com/manual
and
http://www.php.net
alot and you will figure out how.

what you could do, is to loop through the directory for files that you would want to populate in your drop-down list. without javascript, you would select the file you want to update, post (submit) to get that file, open for write and then update with your update.php.


I know this is kinda high-levelish concept, but I am not sure what you are prepared for and I like to help others learn, rather than supply scripts all the time.

btw: I believe the reason why your file was completely written over was the way you opened it. if you use 'w' that means you want to open the file for writing and to start writing at the beginning of the file (it also clears out the file to 0-bytes)
check it out: http://www.zend.com/manual/function.fopen.php