Page 1 of 1

Textarea Editor Editing files online with a textarea Rate Topic: ***-- 2 Votes

#1 SpaceMan  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 10
  • View blog
  • Posts: 507
  • Joined: 20-February 03

Posted 21 December 2004 - 01:42 AM

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.

<?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.

<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

<?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,

<?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>


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 January 2007 - 08:28 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Textarea Editor

#2 unknowtaker  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 31-August 05

Posted 31 August 2005 - 10:39 AM

Thanks alot for this wonderful script man. YOU rock!!
Was This Post Helpful? 0
  • +
  • -

#3 knownasilya  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 148
  • Joined: 11-January 06

Posted 03 February 2006 - 06:49 PM

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.
Was This Post Helpful? 0
  • +
  • -

#4 zubra  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 13-March 06

Posted 13 March 2006 - 03:47 PM

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
Was This Post Helpful? 0
  • +
  • -

#5 cyberscribe  Icon User is offline

  • humble.genius
  • member icon

Reputation: 10
  • View blog
  • Posts: 1,062
  • Joined: 05-May 02

Posted 15 March 2006 - 07:54 PM

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.
Was This Post Helpful? 0
  • +
  • -

#6 golders  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 27-November 06

Posted 27 November 2006 - 04:05 PM

This looks to be just what I need but why does my textarea just contain the text
<?=$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
Was This Post Helpful? 0
  • +
  • -

#7 wizkidweb16  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 03-December 06

Posted 03 December 2006 - 11:04 AM

View Postgolders, on 27 Nov, 2006 - 04:05 PM, said:

This looks to be just what I need but why does my textarea just contain the text
<?=$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:

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:
<!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: 03 December 2006 - 11:07 AM

Was This Post Helpful? 0
  • +
  • -

#8 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1858
  • View blog
  • Posts: 20,275
  • Joined: 17-March 01

Posted 03 December 2006 - 11:34 AM

Are your permissions set properly? I.e. 755??
Was This Post Helpful? 0
  • +
  • -

#9 wizkidweb16  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 03-December 06

Posted 03 December 2006 - 11:40 AM

View Postskyhawk133, on 3 Dec, 2006 - 11:34 AM, said:

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.
Was This Post Helpful? 0
  • +
  • -

#10 SpaceMan  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 10
  • View blog
  • Posts: 507
  • Joined: 20-February 03

Posted 07 January 2007 - 07:43 AM

View Postwizkidweb16, on 3 Dec, 2006 - 11:40 AM, said:

View Postskyhawk133, on 3 Dec, 2006 - 11:34 AM, said:

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.
Was This Post Helpful? 0
  • +
  • -

#11 SpaceMan  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 10
  • View blog
  • Posts: 507
  • Joined: 20-February 03

Posted 07 January 2007 - 08:08 AM

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.

View Postwizkidweb16, on 3 Dec, 2006 - 11:04 AM, said:

View Postgolders, on 27 Nov, 2006 - 04:05 PM, said:

This looks to be just what I need but why does my textarea just contain the text
<?=$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:

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: 07 January 2007 - 08:12 AM

Was This Post Helpful? 0
  • +
  • -

#12 whatdug  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 14-December 07

Posted 14 December 2007 - 08:13 AM

<?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
Was This Post Helpful? 0
  • +
  • -

#13 SpaceMan  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 10
  • View blog
  • Posts: 507
  • Joined: 20-February 03

Posted 15 February 2008 - 08:22 AM

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 February 2008 - 08:30 AM

Was This Post Helpful? 0
  • +
  • -

#14 Auzzie  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 43
  • View blog
  • Posts: 573
  • Joined: 20-January 09

Posted 20 January 2009 - 02:16 PM

This is really good thanks :)
Was This Post Helpful? 0
  • +
  • -

#15 Sabyre  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 21-December 09

Posted 21 December 2009 - 03:41 PM

This is a great little script!

For those of you who are having problems with the script simply refreshing and not writing your changes it may not be a permission issue.

Somewhat unclear in the original instruction, let me clarify.

Add near the top:
$save_file = $_POST['save_file'];  
$savecontent = $_POST['savecontent'];



Like this:
<?php
$file = "mspecs";
$save_file = $_POST['save_file'];  
$savecontent = $_POST['savecontent'];
?>
Edit <?php echo $file; ?></font>


<?php
$loadcontent = $file.".php";
if (!is_writable($loadcontent))
echo 'error';
	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>


This post has been edited by Sabyre: 21 December 2009 - 03:58 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1