I am working on a file editor and I was wondering how I could have the editor, based on the file I choose to edit, - create the form fields automatically. I know I can create each one seperately but I'm sure there is a faster way to do it. I'm still learning so I'm not quite sure what to do.
I will probably have several text input fields and also 2 or 3 textarea fields.
Here is the code, kind of messy but will fix that later.
<?php
//begin
############################################################
?>
<html>
<head>
<title>Edit A File</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style>
<!-- Will add this later -->
</style>
</head>
<body bgcolor="#FFFFFF" text="#333333">
<table width="601" border="0" cellspacing="0" cellpadding="0" height="287">
<tr>
<td height="67" width="110" align="center" valign="middle"> </td>
<td height="67" width="491" align="center" valign="middle" class="admintitle">LOGO</td>
</tr>
<tr>
<td width="110" valign="top" align="left"></td>
<td width="491" align="center" valign="top">
<br>
<table width="90%" border="0" cellspacing="0" cellpadding="0" height="100%">
<tr>
<td align="left" valign="top" height="25" class="admintitle" width="50%">Edit File Module</td>
<td align="right" valign="top" height="25" class="subadmintitle" width="50%">by Matthew Randles</td>
</tr>
<tr>
<td align="left" valign="top" colspan="2">
<?php
if (isset($HTTP_POST_VARS['efileaction'])) {
$efileaction=$HTTP_POST_VARS['efileaction'];
}
if (isset($HTTP_POST_VARS['fname'])) {
$fname=$HTTP_POST_VARS['fname'];
}
if (isset($HTTP_GET_VARS['efileaction'])) {
$efileaction=$HTTP_GET_VARS['efileaction'];
}
if (isset($HTTP_GET_VARS['fname'])) {
$fname=$HTTP_GET_VARS['fname'];
}
if (isset($HTTP_POST_VARS['content'])) {
$content=$HTTP_POST_VARS['content'];
}
if ((isset($fname))and($fname=="efile.php")){
$efileaction="0";
}
if (!isset($efileaction)) {
$efileaction="0";
}
if (($efileaction!="form")and($efileaction!="edit")and($efileaction!="efile")) {
$efileaction="form";
}
if (($efileaction=="edit")and(!$fname)) {
$efileaction="form";
}
if (($efileaction=="efile")and(!$fname)) {
$efileaction="form";
}
if (($efileaction=="edit")and(!file_exists($fname))) {
$efileaction="form";
}
if (($efileaction=="efile")and(!file_exists($fname))) {
$efileaction="form";
}
if ($efileaction=="form") {
?>
<hr width="70%" align="center">
<div class="subadmintitle" align="left">Enter File Name</div><br>
<div align="justify" class="admintext">
Please enter a file that exists! You cannot leave it blank.
<br>
<form method="POST" action="efile.php">
<div class="admintext">Enter File Name</div>
<input type="text" value="" name="fname"><br>
<input type="hidden" value="edit" name="efileaction"><br>
<input type="submit" value="Edit File">
</form>
</div>
<?php
} elseif ($efileaction=="edit") {
?>
<hr width="70%" align="center">
<div class="subadmintitle" align="left">Do Your Editing</div><br>
<div class="admintext" align="left">You are editing <?php echo"$fname"; ?>
<form method="POST" action="efile.php">
<textarea name="fc" rows="12" cols="45">
<?php
if (file_exists($fname)) {
$fp = fopen($fname,"r");
$fc = fread($fp,filesize($fname));
fclose($fp);
echo "$fc";
}
else {
echo "File specified does not exist! or There has been a script error";
}
?>
</textarea>
<input type="hidden" value="<?php echo"$fname"; ?>" name="fname">
<input type="hidden" value="efile" name="efileaction"><br>
<input type="submit" value="Save Changes">
</form>
<form method="POST" action="efile.php">
<input type="hidden" value="<?php echo"$fname"; ?>" name="fname">
<input type="hidden" value="edit" name="efileaction"><br>
<input type="submit" value="Reload (dont save)">
</form>
</div>
<?php
} elseif ($efileaction=="efile") {
//edit actions
if (file_exists($fname)) {
chmod($fname,0755);
$fp = fopen($fname,"w");
$fname = stripslashes($fname);
$fc = htmlspecialchars($fc);
fwrite($fp,$fc);
fclose($fp);
} else {
echo"script error";
}
?>
<hr width="70%" align="center">
<div class="subadmintitle" align="left">Complete</div><br>
<div class="admintext" align="justify">
<?php echo "Congratulations <a href=\"$fname\">$fname</a> has been edited."; ?> Go to your page to verify changes. <br><br>
<form method="POST" action="efile.php">
<input type="hidden" value="form" name="efileaction">
<input type="submit" value="Edit Another File">
</form>
</div>
<?php
} else {
?>
<div align="center" class="scripterror">Script error - action not set to a valid value</div><br><br>
<?php
}
?>
</td>
</tr>
</table>
</td>
</tr>
</table>
<br><br>
</body>
</html>
<?php
//end of content
############################################################
?>