Jump to content

a very simple file editor


blueman378

Recommended Posts

hi there,

im looking to grab a very simple file editor, all it has to do is open a said file (in text format) and read the contents into a text area, then i simple make the changes and click save and it saves them to that file,

e.g

___________________________

|Drop down box to open set files|

 

_____________________________________

|Text area for file to be read to                |

|                                                          |

|____________________________________|

 

___________

|save button|

 

cheers

Link to comment
Share on other sites

There are a fair amount of tutorials out there that you can adapt:

 

You could try something like this, (this hasn't been debugged so may need some tweaking)

 

  <?php
if ($_POST['Submit12'] == "Submit")
{
$file_name = "textfile.txt";
$open_file = fopen($file_name, "a+");
$file_contents= $_POST['title'] . '|' . $_POST['news'];
fwrite($open_file, $file_contents);
fclose($open_file);
echo "Form data successfully written to file";
}
?>

<form id="formname" name="formname" method="post" action="">
  
  Title:</span>
  <input type="text" name="title" />
  
    <textarea name="news" cols="85" rows="15" id="news">
  </textarea>

    <label>
    <input type="submit" name="Submit" value="Submit" />
    </label>

</form>

Link to comment
Share on other sites

yeah sory i should have posted what im currently using:

 

<center><?php
$loadcontent = "textfile.txt";
    $fp = @fopen($loadcontent, "r");
        $loadcontent = fread($fp, filesize($loadcontent));
        $loadcontent = htmlspecialchars($loadcontent);
        fclose($fp);
?>

<form method=post action="<?=$_SERVER['PHP_SELF']?>">
<textarea name="savecontent" cols="25" rows="10"><?=$loadcontent?></textarea>
<br>
<input type="submit" name="save_file" value="Save">  
</form>

<?php
    if($save_file) {
        $savecontent = stripslashes($savecontent);
        $fp = @fopen($loadcontent, "w");
        if ($fp) {
            fwrite($fp, $savecontent);
            fclose($fp);
}
}
?>
</center>

 

this works im just looking for a way to have a dropdown box which contains a list of set files eg :

file1.txt

extra/file2.tx

links.txt

 

and then when the user selects it it writes that value to $loadcontent and refreshes the page so it loads that,

 

i really have no idea where to start for this ( i possibly could but its 11 at night and i cant really think lol

 

thanks for any help,

Link to comment
Share on other sites

sorry,

<?php
include("../include/session.php");
/**
* User not an administrator, redirect to main page
* automatically.
*/
if(!$session->isAdmin()){ ?>
   <center><font color="red"<h3><i>ERROR</i>: Access Denied</h3></font>
Back to [<a href="../index.php">Main Page</a>]</center><?php
}
else{ ?>
<center>
<?php
if (!isset($_POST['submit'])) {
?>
<form method="post" action="<?php echo $PHP_SELF;?>">
Select a file to edit:<br />
<select name="file">
<option value="../links.txt">Site Links</option>
<option value="../mainintro.txt">Main Intro</option>
</select>
<input type="submit" value="submit" name="submit"><br />
</form>
<?php } else {
$loadcontent = $_POST["file"];;
    $fp = @fopen($loadcontent, "r");
        $loadcontent = fread($fp, filesize($loadcontent));
        $loadcontent = htmlspecialchars($loadcontent);
        fclose($fp);
?>
<form method="post" action="<?php echo $PHP_SELF;?>">
Select a file to edit:<br />
<select name="file">
<option value="../links.txt">Site Links</option>
<option value="../mainintro.txt">Main Intro</option>
</select>
<input type="submit" value="submit" name="submit"><br />
</form>
<form method=post action="<?=$_SERVER['PHP_SELF']?>">
<textarea name="savecontent" cols="70" rows="30"><?=$loadcontent?></textarea>
<br>
<input type="submit" name="save_file" value="Save">  
</form>

<?php
    if($save_file) {
        $savecontent = stripslashes($savecontent);
        $fp = @fopen($loadcontent, "w");
        if ($fp) {
            fwrite($fp, $savecontent);
            fclose($fp);
}
}
}
?>
<?php } ?>
</center>

 

there is some extra code there simply to protect the page from someone who is not a admin using it

Link to comment
Share on other sites

ok heres the code

  <?php
/**
* User not an administrator, redirect to main page
* automatically.
*/
if(!$session->isAdmin()){ ?>
   <center><font color="red"><h3><i>ERROR</i>: Access Denied</h3></font>
Back to [<a href="../index.php">Main Page</a>]</center><?php
}
else{ ?>
<center>
<?php
if (!isset($_POST['submit'])) {
?>
<form method="post" action="<?php echo $PHP_SELF;?>">
Select a file to edit:<br />
<select name="file">
<option value="../links.txt">Site Links</option>
<option value="../mainintro.txt">Main Intro</option>
<option value="adminlinks.txt">Admin Links</option>
</select>
<input type="submit" value="submit" name="submit"><br />
</form>
<?php } else {
$loadcontent = $_POST["file"];;
    $fp = @fopen($loadcontent, "r");
        $loadcontent = fread($fp, filesize($loadcontent));
        $loadcontent = htmlspecialchars($loadcontent);
        fclose($fp);
?>
<form method="post" action="<?php echo $PHP_SELF;?>">
Select a file to edit:<br />
<select name="file">
<option value="../links.txt">Site Links</option>
<option value="../mainintro.txt">Main Intro</option>
<option value="adminlinks.txt">Admin Links</option>
</select>
<input type="submit" value="submit" name="submit"><br />
</form>
<form method=post action="<?=$_SERVER['PHP_SELF']?>">
<textarea name="savecontent" cols="70" rows="30"><?=$loadcontent?></textarea>
<br>
<input type="submit" name="save_file" value="Save">  
</form>

<?php
    if($save_file) {
        $savecontent = stripslashes($savecontent);
        $fp = @fopen($loadcontent, "w");
        if ($fp) {
            fwrite($fp, $savecontent);
            fclose($fp);
}
}
}
?>
<?php } ?>

 

now it opens the files alright, however the problem is that it doesnt save to the file when you click save any ideas why?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.