School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 298,436 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,566 people online right now. Registration is fast and FREE... Join Now!




Textarea Editor

 
Reply to this topicStart new topic

> Textarea Editor, Editing files online with a textarea

Rating  5
SpaceMan
Group Icon



post 21 Dec, 2004 - 12:42 AM
Post #1


Seems more and more people are wanting to edit files on-line.
also a common problem in on-line editors is the textarea because editors on-line use the same code,
so it breaks the editor.

first thing is to point it to a file to open,
this part can be done many ways,
but this tutorial does not address those ways or makeing it load different files.

file to open and open it in read mode.

CODE

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


Now we need a way to to view what we just opened.
So we build a form with textarea to make editable content with a button to save it.

CODE

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



Function placement? full code below

CODE

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


so now we have all the parts we need just need them in order so.....

complete code,

CODE

<?php
$loadcontent = "file_name.txt";
    if($save_file) {
        $savecontent = stripslashes($savecontent);
        $fp = @fopen($loadcontent, "w");
        if ($fp) {
            fwrite($fp, $savecontent);
            fclose($fp);
                               }
                }
    $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="70" rows="25"><?=$loadcontent?></textarea>
<br>
<input type="submit" name="save_file" value="Save">  
</form>


CODE

if you have problems with the server register globals being off,
is 2 ways to fix this.
1) add at the top for all the post feilds in the form,
$save_file = $_POST['save_file'];  
$savecontent = $_POST['savecontent'];
for each one.
2) or change them all to ,
    if($_POST['save_file']) {
        $savecontent = stripslashes($_POST['savecontent']);



A while back i started an online editor
i use on many sites and have added some nice features like,
line #, search, replace, file renameing and some other things.
EasyPHPEdit



Have Fun
Dave

This post has been edited by SpaceMan: 23 Jan, 2007 - 07:28 PM
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

unknowtaker
*



post 31 Aug, 2005 - 09:39 AM
Post #2
Thanks alot for this wonderful script man. YOU rock!!
Go to the top of the page
+Quote Post

knownasilya
Group Icon



post 3 Feb, 2006 - 05:49 PM
Post #3
Im going to add this to my Mercury Board backend so I can edit CSS without always having to FTP. Thanks, this is very helpfull.
Go to the top of the page
+Quote Post

zubra
*



post 13 Mar, 2006 - 02:47 PM
Post #4
Hi,

You are right - more and more people want to edit their files online. And your script is very handy for this, but textarea is not very suitable for editing of source code files. If you want to enjoy professional-grade features, while editing your files online, check this browser-based text and source code editor:

http://aboutedit.com

Among its features are syntax-highlighting for many languages, multiple document editing, smart tabs and indents and more. Take a look.

Best Regards,
Zubra
Go to the top of the page
+Quote Post

cyberscribe
Group Icon



post 15 Mar, 2006 - 06:54 PM
Post #5
Kinda cute JS/PHP client/server idea. But my cursor was WAY off where it was actually editing using FF on OS X. Plus I had to ctrl+a instead of command+a to hilight all text. Close, but not a desktop replacement.
Go to the top of the page
+Quote Post

golders
*



post 27 Nov, 2006 - 03:05 PM
Post #6
This looks to be just what I need but why does my textarea just contain the text
CODE
<?=$loadcontent?>


I changed the filename to be that of an html snippet file on my server which is in the same folder as this new edit page. For some reason the file is not getting read.

When I change the text in the textarea to something/anything and click the save button I get an error that explorer cannot display the webpage.

I'm hoping this is something fundamental :-) Cos this is exactly the functionality I need to allow editting of content on my site.

cheers
Golders

Go to the top of the page
+Quote Post

wizkidweb16
*



post 3 Dec, 2006 - 10:04 AM
Post #7
QUOTE(golders @ 27 Nov, 2006 - 04:05 PM) *

This looks to be just what I need but why does my textarea just contain the text
CODE
<?=$loadcontent?>


I changed the filename to be that of an html snippet file on my server which is in the same folder as this new edit page. For some reason the file is not getting read.

When I change the text in the textarea to something/anything and click the save button I get an error that explorer cannot display the webpage.

I'm hoping this is something fundamental :-) Cos this is exactly the functionality I need to allow editting of content on my site.

cheers
Golders

Could you possibly be using HTML? If you are using HTML, this script will not work. blink.gif

I am using the complete code that was posted last on the tutorial, and I can view the code for my file fine, but I cannot save it. It refreshes the page and keeps the original file content. I am also using a program called TinyMCE, but that just formatts the textbox. When submitting, it posts the html. My code is as follows:
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>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<link href="../css/pagedesign.css" rel="stylesheet" type="text/css" />
</head>
<script language="javascript" type="text/javascript" src="tinymce/jscripts/tiny_mce/tiny_mce.js"></script>
<script language="javascript" type="text/javascript">
tinyMCE.init({
    theme : "simple",
    mode : "textareas"
});
</script>
<body>
<div align="center">
  <p>Edit News Page</p>
  <p>------------------</p>
  <p><?php
$loadcontent = "../whats_hot.php";
    if($save_file) {
        $savecontent = stripslashes($savecontent);
        $fp = @fopen($loadcontent, "w");
        if ($fp) {
            fwrite($fp, $savecontent);
            fclose($fp);
}
}
    $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="70" rows="25"><?=$loadcontent?></textarea>
<br>
<input type="submit" name="save_file" value="Save">  
</form>  </p>
</div>
</body>
</html>


This post has been edited by wizkidweb16: 3 Dec, 2006 - 10:07 AM
Go to the top of the page
+Quote Post

skyhawk133
Group Icon



post 3 Dec, 2006 - 10:34 AM
Post #8
Are your permissions set properly? I.e. 755??
Go to the top of the page
+Quote Post

wizkidweb16
*



post 3 Dec, 2006 - 10:40 AM
Post #9
QUOTE(skyhawk133 @ 3 Dec, 2006 - 11:34 AM) *

Are your permissions set properly? I.e. 755??

well, in the same folder, i am able to upload files with a form, so I don't think its a problem with permissions.
Go to the top of the page
+Quote Post

SpaceMan
Group Icon



post 7 Jan, 2007 - 06:43 AM
Post #10
QUOTE(wizkidweb16 @ 3 Dec, 2006 - 11:40 AM) *

QUOTE(skyhawk133 @ 3 Dec, 2006 - 11:34 AM) *

Are your permissions set properly? I.e. 755??

well, in the same folder, i am able to upload files with a form, so I don't think its a problem with permissions.


same folder wont mater if uploaded the file via ftp, it must be 0666 at least to write to it.
in the posted code, $loadcontent = "../whats_hot.php";
is one dir down.
Go to the top of the page
+Quote Post

SpaceMan
Group Icon



post 7 Jan, 2007 - 07:08 AM
Post #11
would try 2 things, one the register globals maybe set to off, and/or older php suports <?= tags? not sure on this, but easy fix.

i added fix on the tut for the globals.
change,<?=$loadcontent?>
to, <?php echo $loadcontent; ?>

as for the permisions, easy check,

where it says,
if ($fp) {
fwrite($fp, $savecontent);
fclose($fp);
}
change to,

if ($fp) {
fwrite($fp, $savecontent);
fclose($fp);
} else
echo "Error opening file";

@fopen will surpress error reporting, can take off the @ before the function and will output errors to brouser if your server is to display them.
but this will tell you eather way.

QUOTE(wizkidweb16 @ 3 Dec, 2006 - 11:04 AM) *

QUOTE(golders @ 27 Nov, 2006 - 04:05 PM) *

This looks to be just what I need but why does my textarea just contain the text
CODE
<?=$loadcontent?>


I changed the filename to be that of an html snippet file on my server which is in the same folder as this new edit page. For some reason the file is not getting read.

When I change the text in the textarea to something/anything and click the save button I get an error that explorer cannot display the webpage.

I'm hoping this is something fundamental :-) Cos this is exactly the functionality I need to allow editting of content on my site.

cheers
Golders

Could you possibly be using HTML? If you are using HTML, this script will not work. blink.gif

I am using the complete code that was posted last on the tutorial, and I can view the code for my file fine, but I cannot save it. It refreshes the page and keeps the original file content. I am also using a program called TinyMCE, but that just formatts the textbox. When submitting, it posts the html.


This post has been edited by SpaceMan: 7 Jan, 2007 - 07:12 AM
Go to the top of the page
+Quote Post

whatdug
*



post 14 Dec, 2007 - 07:13 AM
Post #12
CODE

<?php
$what = $_GET['page'];

?>
<font face="Georgia">Edit <?php echo $what; ?></font>


<?php
$loadcontent = "../".$what.".php";
    if($save_file) {
        $savecontent = stripslashes($savecontent);
        $fp = @fopen($loadcontent, "w");
        if ($fp) {
        echo 'written';
            fwrite($fp, $savecontent);
            fclose($fp);
                               }
                }
    $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="70" rows="25"><?=$loadcontent?></textarea>
<br>
<input type="submit" name="save_file" value="Save">  
</form>
<p><a href="java script:history.go(-1)">Back</a></p>




Why doesn't this save changes even though I have set permissions 777 but it keeps reverting to 755
Go to the top of the page
+Quote Post

SpaceMan
Group Icon



post 15 Feb, 2008 - 07:22 AM
Post #13
hope you have login, security before the $what.

usually i dont edit content as php.
text files, then include them, will resove some issues *nix permissions.

is not a good idea to have writable files above the http server root.

you can add
if (!is_writable($loadcontent))
echo 'error';
right before the if($save_file) {
to see if the file will work on first page load.

i have seen *nix change pemissions on some shared servers.

examle .txt content. 0777 dir, 0666 page
$loadcontent = "../../pages/".$what.".txt";

loading original page.
include_once '../../pages/page_name.txt';

can stop a few problems with permissions.



This post has been edited by SpaceMan: 15 Feb, 2008 - 07:30 AM
Go to the top of the page
+Quote Post

Auzzie
Group Icon



post 20 Jan, 2009 - 01:16 PM
Post #14
This is really good thanks smile.gif
Go to the top of the page
+Quote Post


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/3/09 09:45PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month