?
Solved

open/edit/save text files in form text area.

Posted on 2003-11-10
3
Medium Priority
?
1,911 Views
Last Modified: 2006-11-17
I am in desparate need of a page that has a list of text files that can be edited (I can do that). The page I can't do is the one to open the chosen text file (through querystring $_GET['file']) into a form textarea field, allow it to be edited, and then saved back over the top of the existing file. Preferably it would create a backup of the file it is re-saving.

Thanks.
0
Comment
Question by:ralphuk100
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 33

Accepted Solution

by:
snoyes_jw earned 380 total points
ID: 9716809
page1.php:

<form action="page2.php">
<input name = "fileName" type="hidden" value="<?php echo $filename?>" method="POST">
<textarea name="somecontent">
<?php
// read the file contents
$filename = $_GET['file'];
$handle = fopen($filename, 'r');
echo fread ($handle, filesize ($filename));
fclose ($handle);
?>
</textarea>
</form>


page2.php:

<?php
// backup the file
copy ($_POST'fileName'], $_POST['fileName'] . "backup");
// write over the file
$handle = fopen($filename, 'w');
fwrite($handle, $_POST['somecontent']);
fclose($handle);
?>
0
 
LVL 33

Expert Comment

by:snoyes_jw
ID: 9716817
Sorry, the method=post should go in the form tag, not the hidden input tag, and $filename won't be defined until after the $filename = $_GET['file'], so move the hidden input tag to after the text area.
0
 
LVL 6

Expert Comment

by:aolXFT
ID: 9718153
I recommend adding a htmlentities() to snoyes_jw's code, since his has XSS vulnerabilities.

If you have a page with the following contents(although you shouldn't), it will cause problems:

##############################

</textarea>

<script language="javascript">
<!--
do_bad_evil_stuff();

//-->
</script>

##############################

You are going to have problems.

to get around this problem, you can htmlspecialchars the contents of the textarea.

replace

echo fread ($handle, filesize ($filename));

with

echo htmlspecialchars(fread ($handle, filesize ($filename)));
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question