Log in

View Full Version : editing a file online



bradenkeithcom
01-16-2008, 05:29 PM
the scenario is that i want to set up a page where my customer can go and update the news section of his website on his own. I'm having a hard time opening and saving the .txt file called news. here's my script:


<?php
$loadcontent = "files/news.txt";
if($save_file) {
$savecontent = stripslashes($savecontent);
$fp = @fopen($loadcontent, "w");
if ($fp) {
fwrite($fp, $savecontent);
fclose($fp);
print '<a href='.$_SERVER[PHP_SELF].'>Refresh</a>';
print "<html><head><META http-equiv=\"refresh\" content=\"0;URL=$_SERVER[PHP_SELF]\"></head><body>";

}
}
$fp = @fopen($loadcontent, "r");
$loadcontent = fread($fp, filesize($loadcontent));
$lines = explode("\n", $loadcontent);
$count = count($lines);
$loadcontent = htmlspecialchars($loadcontent);
fclose($fp);
for ($a = 1; $a < $count+1; $a++) {
$line .= "$a\n";
}
?>
<form method=post action="<?=$_SERVER[PHP_SELF]?>">
<input type="submit" name="save_file" value="Save">
<table width="100%" valign="top" border="0" cellspacing="1" cellpadding="1">
<tr>
<td width="3%" align="right" valign="top"><pre style="text-align: right; padding: 4px; overflow: auto; border: 0px groove; font-size: 12px" name="lines" cols="4" rows="<?=$count+3;?>"><?=$line;?></pre></td>
<td width="97%" align="left" valign="top"><textarea style="text-align: left; padding: 0px; overflow: auto; border: 3px groove; font-size: 12px" name="savecontent" cols="150" rows="<?=$count;?>" wrap="OFF"><?=$loadcontent?></textarea></td>
</tr>
</table>

<br>
<input type="submit" name="save_file" value="Save">
</form>

all i'm gettin right now is the <?=$loadcontent?> instead the actual text; so some help would be highly appreciated.

thetestingsite
01-16-2008, 06:14 PM
Try



<?php echo $loadcontent; ?>


instead of


<?=$loadcontent?>


Also, don't use global variables, but instead use $_GET, $_POST, $_SERVER, etc global arrays to call your variables.

Hope this helps.

Master_script_maker
01-16-2008, 10:26 PM
you might also want to change this:

<form method=post action="<?=$_SERVER[PHP_SELF]?>">
to this:

<form method=post action="<?php echo $_SERVER[PHP_SELF]; ?>">