Help - Search - Members - Calendar
Full Version: Edit a file online
Invision Power Services > Community Forums > Community Web Design and Coding
FCB-Mo
Does anyone know how to edit a file online in a textarea? If so, how do you do it, cos i really need it.
Brendon Koz
Need to make sure the file has permissions to be modified on the server. Use the file system functions, namely open with read, then close...output this between your textarea tags.
http://us2.php.net/manual/en/ref.filesystem.php <-- File system functions

Upon submitting the form, you take the text within the textarea and overwrite the old file's contents. See the file system functions for what to use.

There's always security implications with any of this, but I'll leave you to discover those on your own as I think you'll be more apt to remember them that way.
FCB-Mo
umm........... Could you write it up for me please, cos this is a tad too advanced for me.....

as for the security, this page is in a password protected directory so only those who are meant to edit the pages can edit them.
Brendon Koz
I personally don't really have the time to do so.

If you're looking for something to manage pages, it might be easier to use a third-party script (such as Stephen's File Manager http://www.rebelinblue.com/fm as opposed to having it within your own site).
FCB-Mo
i only want to edit 1 or 2 pages so there is no point in using a file manager + since ive already built all of my admin panel already, all i need is to be able to edit these pages and im done
Brendon Koz
I'll see if I can write up a quick example later on.
FCB-Mo
thank you original.gif
mandos
$fh = fopen('myfile.txt.', 'r+');
fwrite($fh, "mynewcontent");
Scott B
QUOTE(mandos @ Mar 25 2006, 10:58 PM) *
$fh = fopen('myfile.txt.', 'r+');
fwrite($fh, "mynewcontent");


That is of little use to him.

Is too late to be coding for me so here's a quick guide:

1) Open the file you wish to edit using php file functions (fread)
2) Use str_replace to replace & with &amp;, < with &lt; and > with &gt (if you are editing HTML)
3) Put that replaced string in a textarea
4) Process the form, and use php file functions (fwrite) to write the contents of that form to the file you wish to edit.

Edit: Actually, here's a very quick (and unsafe) guide as to how do it:
CODE
<?php

$file = "me.html";

if (!isset($_POST['submit']))
{
  $fo = fopen($file, "r");
  $fr = fread($fo, filesize($file));
  
  $fr = str_replace("&", "&amp;", $fr);
  $fr = str_replace("<", "&lt;", $fr);
  $fr = str_replace(">", "&gt;", $fr);
  
  echo "<form method='post' action='{$_SERVER['PHP_SELF']}'>
        <textarea name='newfile' rows='10' cols='50'>{$fr}</textarea>
        <br />
        <input type='submit' name='submit' value='Submit' />
        </form>";
  fclose($fo);
}
else
{
  $fo = fopen($file, "w");
  $fw = fwrite($fo, $_POST['newfile']);
  fclose($fo);
  
}

?>


Should all be pretty self explanatory original.gif
Brendon Koz
Ugh..I haven't gotten to bed yet. That's about as much as I was going to do. If you need that expanded, let us know.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.