Solved

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

Posted on 2003-11-10
3
1,908 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
  • 2
3 Comments
 
LVL 33

Accepted Solution

by:
snoyes_jw earned 95 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
what do I need to host my own web sites? 13 51
Need to remove %20 from url in get 17 36
Wordpress Pagination 1 29
Correct syntax for subdirectories 9 20
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

828 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