[HTML or PHP] Include a File, Edit, and Save?

Joined
Dec 4, 2007
Messages
1,935
Reaction score
96
You know like vBulletin? The template system. Well, on my forum software, I want something similar, but, I want it so in the Administrator Control Panel, you can go to the "templates" page, and select the template you want to edit, and say you choose "header" .. Then it loads that page with an edit box, and with all the contents of "themes/default/header.html" in it, and when you save it, it goes back to that file, and saves.

Is there anyway I could do this? In PHP or HTML, or whatever. Whatever language I can do this in would be great.
 
A basic method will consist of 2 modules which in this case are the: opening and collecting html contents, and saving edited contents.

As for reading the html contents, simply, use 'file_get_contents'. Do not forget to set a stand-alone variable for handling the edited contents.

As for saving the edited content you may use 'fwrite' function; a basic clear tutorial can be found here: http://php.about.com/od/advancedphp/ss/file_write_php_2.htm Also, make sure you integrate wysiwyg within your implementation to make things plain and simple for client's interface.
 
Somewhere within these lines you might consider inheriting:
Code:
<?php
      if(isset($_POST['submit']))
          {
            $file = "file.html";
            $handle = fopen($file, 'w');
            $data = $_POST['string'];
            fwrite($handle, $data);
            print "Data Written";
            fclose($handle);
          }
      $file = "file.html";
      $data = file_get_contents($file);
?>
<form action="<? echo $_SERVER['PHP_SELF']; ?>" method="post">
<textarea name="string"><? echo "$data"; ?></textarea>
<input name="submit" type="submit" value="Amend">
</form>
 
Code:
<?php
      if(isset['submit']) 
          {
            $file = "file.html";
            $handle = fopen($file, 'w');
            $data = $_POST['string'];
            fwrite($handle, $data);
            $data = $_POST['string'];
            fwrite($handle, $data);
            print "Data Written";
            fclose($handle);
          }
      $file = "file.html";
      $data = file_get_contents($file);
?>
<form action="<? echo $_SERVER['PHP_SELF']; ?>" method="post">
<textarea name="string"><? echo "$data"; ?></textarea>
<input name="submit" type="submit" value="Amend">
</form>

Posting a code means nothing to me.

It means a lot that you're trying to help, but please, explain what it does, so I can learn?

edit: is that supposed to open the file contents, and then you can submit it when you're done? will it fully work? (save back to the original file)
 
Well, by this stage that you have had a good approach towards creating a framework of a forum, I presumed you can read along the lines of these codes.

However, in theory if your an extrovert then in that case I'll explain the figure which is presented with annotations below.

The processor; the comments explains:
Code:
<?php
     [B]//Figure 1: when the input has been submitted, receives the 'isset-submit' process[/B]:
      if(isset($_POST['submit']))
          {
            $file = "file.html"; [B]//A variable which handles the name of the file to search.[/B]
            $handle = fopen($file, 'w'); [B]//Opens the file which ever the value for $file is.[/B]
            $data = $_POST['string']; [B]//Defines the $data handling the received request of content to change into.[/B]
            fwrite($handle, $data); [B]//Writes the data onto $file.[/B]
            print "Data Written"; [B]//Print the contents for confirmation.[/B]
            fclose($handle); [B]//Closes the process to end the loop.[/B]
          }
     
      [B]//Figure 2: on the other hand, if the input has not been submitted from the form the line continues to its read:[/B]
      $file = "file.html"; [B]//Defines the file to search[/B]
      $data = file_get_contents($file); [B]//Gather contents of whichever the value for $file is.[/B]
?>

The form; the comments explains:
Code:
<form action="<? echo $_SERVER['PHP_SELF']; ?>" method="post">
<textarea name="string"><? echo "$data"; [B]//Prints the value of variable: $data, from figure 2.[/B] ?></textarea>
<input name="submit" type="submit" value="Amend">
</form>
 
You shouldn't be using:

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

It can be altered and therefor exploited. If it is handled within the same file you don't have to use action :)
 
You shouldn't be using:

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

It can be altered and therefor exploited. If it is handled within the same file you don't have to use action :)

That isn't really unsafe, unless you use improper form handling which would make your site unsafe even without the action.
 
I tried doing what you said IQstim, I added this code...

Code:
<?php
      if(isset['submit']) 
          {
            $file = "header.htm";
            $handle = fopen($file, 'w');
            $data = $_POST['string'];
            fwrite($handle, $data);
            $data = $_POST['string'];
            fwrite($handle, $data);
            print "Data Written";
            fclose($handle);
          }
      $file = "header.htm";
      $data = file_get_contents($file);
?>
<form action="<? echo $_SERVER['PHP_SELF']; ?>" method="post">
<textarea name="string"><? echo "$data"; ?></textarea>
<input name="submit" type="submit" value="Submit">
</form>

To a completely empty .html page, and when I visit it in my browser, I get this...

Mike - [HTML or PHP] Include a File, Edit, and Save? - RaGEZONE Forums


What's wrong with it?
 
I tried doing what you said IQstim, I added this code...

Code:
<?php
      if(isset['submit']) 
          {
            $file = "header.htm";
            $handle = fopen($file, 'w');
            $data = $_POST['string'];
            fwrite($handle, $data);
            $data = $_POST['string'];
            fwrite($handle, $data);
            print "Data Written";
            fclose($handle);
          }
      $file = "header.htm";
      $data = file_get_contents($file);
?>
<form action="<? echo $_SERVER['PHP_SELF']; ?>" method="post">
<textarea name="string"><? echo "$data"; ?></textarea>
<input name="submit" type="submit" value="Submit">
</form>
To a completely empty .html page, and when I visit it in my browser, I get this...

Mike - [HTML or PHP] Include a File, Edit, and Save? - RaGEZONE Forums


What's wrong with it?
Short tags dont work on all host's have you tried <?php as in the main piece of code.
 
It shows the exact same thing, but in the text box, now just shows the code

Code:
<?php echo "$data"; ?>

Instead of...

Code:
<? echo "$data"; ?>

What else can I do?


edit: I don't know if this means anything... But when I click "Submit" randomly, it just shows an error page with...

Error 404!

/website/themes/default/<
File Not Found!

why is it looking for file "<"..?


edit2: in the address bar of that error page, it shows "http://trasion.com/website/themes/default/%3C?php%20echo%20$_SERVER[%27PHP_SELF%27];%20?%3E"

I guess that's what's causing it to look for "<"?
 
Mike this works fine on my webserver:
PHP:
<?php
      if(($_POST['submit'])) 
          {
            $file = "header.htm";
            $handle = fopen($file, 'w');
            $data = $_POST['string'];
            fwrite($handle, $data);
            $data = $_POST['string'];
            fwrite($handle, $data);
            print "Data Written";
            fclose($handle);
          }
      $file = "header.htm";
      $data = file_get_contents($file);
?>
<form action="<? echo $_SERVER['PHP_SELF']; ?>" method="post">
<textarea name="string"><? echo "$data"; ?></textarea>
<input name="submit" type="submit" value="Submit">
</form>
 
Edit: I spoke too soon...

It opens a text box with this:

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>$title</title>
<meta http-equiv="keywords" content="$keyword" />
<meta http-equiv="description" content="$description" />
<meta http-equiv="Content-Type" content="text/html; charset=$encoding" />
</head>
<body>

<script src="languages/$language.js" type="text/javascript"></script>
<script src="javascript/aliboard.js" type="text/javascript"></script>
<script type="text/javascript">
	<!-- JAVASCRIPT_CODES -->
</script>

<link href="themes/default/stylesheet/main-style.css" rel="stylesheet" type="text/css" />

<div style="width:auto;" align="center">
<table width="100%" cellspacing="0" cellpadding="0" border="0">
	<tr>
		<td>
	
	
	<center>
	<div class="header"></div>
	<div class="navigation_border">
<div id="mytabsmenu" class="tabsmenuclass">
<ul>
<li><a href="http://www.trasion.com/index.html" rel="gotsubmenu">Homepage</a></li>
<li><a href="http://www.trasion.com/communityforum/" rel="gotsubmenu">Community</a></li>
<li><a href="http://www.trasion.com/software.html" rel="gotsubmenu">Software</a></li>
<li><a href="http://www.trasion.com/customise.html" rel="gotsubmenu">Customise</a></li>
<li><a href="http://www.trasion.com/supportforum/" rel="gotsubmenu">Blogs</a></li>
<li><a href="http://www.trasion.com/developers.html" rel="gotsubmenu">Developers</a></li>
<li><a href="http://www.trasion.com/contactus.html" rel="gotsubmenu">Contact Us</a></li>
</ul>
</div>
</div>
<div class="tabsmenucontentclass">
 </div></center>

<div class="content_border" style="text-align:left"><div class="content">


	<!-- BEGIN_TOP_LINKS -->
	<!-- BEGIN_GUEST_TOP_LINKS -->
	<div style="width:auto;border:#737373 solid 1px;border-top:0px;border-bottom:0px;padding:8px;padding-left:4px;padding-right:4px;font-family:tahoma;font-size:8pt;height:14px;background-color:#EEEEEE;" align="left">
		<span style="margin-left:10px;float:left"><b><a href="search.php">Search</a></b> | <b>$login_link</b> | Current Time is $current_time</span>
		<span style="margin-right:10px;float:right"><b>$register_link</b></span>
	</div>
	
	<div class="navigate" style="width:auto;border:#737373 solid 1px;border-top:0px;padding:4px;font-family:tahoma;font-size:8pt;height:14px;background-color:#e6e6e6">
	<span style="margin-left:10px;font-weight:bold;float:left">$navigate</span>
	<span style="margin-right:10px;font-weight:bold;float:right"></span>
	</div>
	<!-- END_GUEST_TOP_LINKS -->
	
	<!-- BEGIN_REGISTERED_TOP_LINKS -->
	<div style="width:auto;border:#737373 solid 1px;border-top:0px;border-bottom:0px;padding:8px;padding-left:4px;padding-right:4px;font-family:tahoma;font-size:8pt;height:14px;background-color:#EEEEEE;" align="left">
	
		
		<div align="right" style="width:auto;height:14px;">
		<span style="margin-left:10px;float:left"><b><a href="search.php">Search</a></b> | <b>$user_cp_link</b>
		 | Current Time is $current_time</span>
		<span style="float:right;margin-right:10px;"><b>$signout_link</b></span>
		</div>
				
</div>
<div class="navigate" style="width:auto;border:#737373 solid 1px;border-top:0px;padding:4px;font-family:tahoma;font-size:8pt;height:14px;background-color:#e6e6e6; background-position: bottom;background-repeat:repeat-x" align="left">
	<span style="margin-left:10px;font-weight:bold;float:left">$navigate</span>
	<span style="margin-right:10px;font-weight:bold;float:right"></span>
	</div>
	<!-- END_REGISTERED_TOP_LINKS -->

	<!-- END_TOP_LINKS -->





	<!-- BEGIN_ERROR_REPORT -->
	<div class="error_div" align="left">
		<!-- BEGIN_ERROR_ITEM -->
			<li>$error_item</li>
		<!-- END_ERROR_ITEM -->
	</div>
	<!-- END_ERROR_REPORT -->
	
	
	<!-- BEGIN_DONE_REPORT -->
	<div class="done_div" align="left">
		<!-- BEGIN_DONE_ITEM -->
			<li>$error_item</li>
		<!-- END_DONE_ITEM -->
	</div>
	<!-- END_DONE_REPORT -->

If I edit it, it changes to:

Code:
<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
<html xmlns=\"http://www.w3.org/1999/xhtml\">
<head>
<title>site- $title</title>
<meta http-equiv=\"keywords\" content=\"$keyword\" />
<meta http-equiv=\"description\" content=\"$description\" />
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=$encoding\" />
</head>
<body>

<script src=\"languages/$language.js\" type=\"text/javascript\"></script>
<script src=\"javascript/aliboard.js\" type=\"text/javascript\"></script>
<script type=\"text/javascript\">
	<!-- JAVASCRIPT_CODES -->
</script>

<link href=\"themes/default/stylesheet/main-style.css\" rel=\"stylesheet\" type=\"text/css\" />

<div style=\"width:auto;\" align=\"center\">
<table width=\"100%\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\">
	<tr>
		<td>
	
	
	<center>
	<div class=\"header\"></div>
	<div class=\"navigation_border\">
<div id=\"mytabsmenu\" class=\"tabsmenuclass\">
<ul>
<li><a href=\"http://www.trasion.com/index.html\" rel=\"gotsubmenu\">Homepage</a></li>
<li><a href=\"http://www.trasion.com/communityforum/\" rel=\"gotsubmenu\">Community</a></li>
<li><a href=\"http://www.trasion.com/software.html\" rel=\"gotsubmenu\">Software</a></li>
<li><a href=\"http://www.trasion.com/customise.html\" rel=\"gotsubmenu\">Customise</a></li>
<li><a href=\"http://www.trasion.com/supportforum/\" rel=\"gotsubmenu\">Blogs</a></li>
<li><a href=\"http://www.trasion.com/developers.html\" rel=\"gotsubmenu\">Developers</a></li>
<li><a href=\"http://www.trasion.com/contactus.html\" rel=\"gotsubmenu\">Contact Us</a></li>
</ul>
</div>
</div>
<div class=\"tabsmenucontentclass\">
 </div></center>

<div class=\"content_border\" style=\"text-align:left\"><div class=\"content\">


	<!-- BEGIN_TOP_LINKS -->
	<!-- BEGIN_GUEST_TOP_LINKS -->
	<div style=\"width:auto;border:#737373 solid 1px;border-top:0px;border-bottom:0px;padding:8px;padding-left:4px;padding-right:4px;font-family:tahoma;font-size:8pt;height:14px;background-color:#EEEEEE;\" align=\"left\">
		<span style=\"margin-left:10px;float:left\"><b><a href=\"search.php\">Search</a></b> | <b>$login_link</b> | Current Time is $current_time</span>
		<span style=\"margin-right:10px;float:right\"><b>$register_link</b></span>
	</div>
	
	<div class=\"navigate\" style=\"width:auto;border:#737373 solid 1px;border-top:0px;padding:4px;font-family:tahoma;font-size:8pt;height:14px;background-color:#e6e6e6\">
	<span style=\"margin-left:10px;font-weight:bold;float:left\">$navigate</span>
	<span style=\"margin-right:10px;font-weight:bold;float:right\"></span>
	</div>
	<!-- END_GUEST_TOP_LINKS -->
	
	<!-- BEGIN_REGISTERED_TOP_LINKS -->
	<div style=\"width:auto;border:#737373 solid 1px;border-top:0px;border-bottom:0px;padding:8px;padding-left:4px;padding-right:4px;font-family:tahoma;font-size:8pt;height:14px;background-color:#EEEEEE;\" align=\"left\">
	
		
		<div align=\"right\" style=\"width:auto;height:14px;\">
		<span style=\"margin-left:10px;float:left\"><b><a href=\"search.php\">Search</a></b> | <b>$user_cp_link</b>
		 | Current Time is $current_time</span>
		<span style=\"float:right;margin-right:10px;\"><b>$signout_link</b></span>
		</div>
				
</div>
<div class=\"navigate\" style=\"width:auto;border:#737373 solid 1px;border-top:0px;padding:4px;font-family:tahoma;font-size:8pt;height:14px;background-color:#e6e6e6; background-position: bottom;background-repeat:repeat-x\" align=\"left\">
	<span style=\"margin-left:10px;font-weight:bold;float:left\">$navigate</span>
	<span style=\"margin-right:10px;font-weight:bold;float:right\"></span>
	</div>
	<!-- END_REGISTERED_TOP_LINKS -->

	<!-- END_TOP_LINKS -->





	<!-- BEGIN_ERROR_REPORT -->
	<div class=\"error_div\" align=\"left\">
		<!-- BEGIN_ERROR_ITEM -->
			<li>$error_item</li>
		<!-- END_ERROR_ITEM -->
	</div>
	<!-- END_ERROR_REPORT -->
	
	
	<!-- BEGIN_DONE_REPORT -->
	<div class=\"done_div\" align=\"left\">
		<!-- BEGIN_DONE_ITEM -->
			<li>$error_item</li>
		<!-- END_DONE_ITEM -->
	</div>
	<!-- END_DONE_REPORT -->

And messes up the whole website header...
 
I tried doing what you said IQstim, I added this code...

Code:
<?php
      if(isset['submit']) 
          {
            $file = "header.htm";
            $handle = fopen($file, 'w');
            $data = $_POST['string'];
            fwrite($handle, $data);
            $data = $_POST['string'];
            fwrite($handle, $data);
            print "Data Written";
            fclose($handle);
          }
      $file = "header.htm";
      $data = file_get_contents($file);
?>
<form action="<? echo $_SERVER['PHP_SELF']; ?>" method="post">
<textarea name="string"><? echo "$data"; ?></textarea>
<input name="submit" type="submit" value="Submit">
</form>
To a completely empty .html page, and when I visit it in my browser, I get this...

Mike - [HTML or PHP] Include a File, Edit, and Save? - RaGEZONE Forums


What's wrong with it?
PHP is not executable on HTML documents (.html), thus the reason you must save is as PHP document (.php).
 
Back