Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to edit files using a cgi script??

Status
Not open for further replies.

jamsman

Technical User
Jul 22, 2005
38
DE
hi,
I am writing a cgi script that needs to beable to open a tet file and then the user edit the content and then save the file is this possbile using perl through cgi??

any ideas would be a great help

cheers
 
yes - the text file will have to be on the server though

how's this for starters:-

Code:
[b]#!/usr/bin/perl[/b]

print "Content-type: text/html\n\n";

read (STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

if ($buffer) {
  print "We have POST data...<br>";
  print $buffer;
}

open (IN, "< user_editable.txt");
undef $/;
$_ = <IN>;
$/ = "\n";
close IN;

print <<HTML;
<form action=$0 method=POST>
  <textarea cols=50 rows=10 name=editfield>$_</textarea>
  <input type=submit>
</form>
HTML
# end of script


Kind Regards
Duncan
 
cheers i think thats exactly what i was after :)
 
no problem. obviously you will need to translate the URI characters and then write some code to save the file etc.

if you need any further help please shout


Kind Regards
Duncan
 
if it's a text file on the server:

Code:
print the beginning of your form

open(FH,$filename) or die "Unable to open $filename: $!";
print qq~textarea name="edit" cols="10" rows="10">~;
print while (<FH>);
print qq~</textarea>~;

print the end of your form

beaware, if there is html code in the file being edited it will affect the textarea tag used to edit the file, like if </textarea> is in the file it will close the textarea tag you opened to edit the file! html tag brackets should be escaped before printing them into the textarea tag for editing, and double quotes and ampersands should be escaped too if need be.
 
(not intending to be rude!)

Code:
open(FH,$filename) or die "Unable to open $filename: $!";
print qq~[b][red]<[/red][/b]textarea name="edit" cols="10" rows="10">~;
print while (<FH>);
print qq~</textarea>~;
[b][red]close FH;[/red][/b]


Kind Regards
Duncan
 
thanks Duncan,

corrections or apmlifications are always appreciated.
 
Hi Kevin

No problem - PLEASE don't consider it a dig!!! :)


Kind Regards
Duncan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top