CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Writing form data to txt file...

 



Xentic
stranger

Nov 5, 2001, 5:40 AM

Post #1 of 6 (949 views)
Writing form data to txt file... Can't Post

HI Guys!

I'm using a little cgi script to add some info to a text file. I think this is the script that writes it into the file:

#open your file for writing, write to it then close file
die ("Cannot open your datafile\n") unless
open (FILE, ">>$datafile");
print FILE "$addthistofile\n";
close(FILE);

Every time i add something the new data is below the old data. Does anyone know how to get it on top of the old content?

Thanks!

Michiel



fkheiri
stranger

Nov 5, 2001, 7:27 AM

Post #2 of 6 (945 views)
Re: Writing form data to txt file... [In reply to] Can't Post

The most reliable way is to create a new text file, with the new data first, followed by the old data. Then, you could either rename the new file and delete the old file, or just delete the old file and not bother about renaming the new one. The logic goes something like this:

1. create new text file, say NEWFILE
2. print NEWFILE "$your new data
3. open old file, say OLDFILE
4. print NEWFILE "$data from OLDFILE"
5. close OLDFILE
6. close NEWFILE

So now, NEWFILE has your new data first, and the old data below.



fashimpaur
User / Moderator

Nov 5, 2001, 7:33 AM

Post #3 of 6 (945 views)
Re: Writing form data to txt file... [In reply to] Can't Post

Sure. first, you cannot append to the file. Therefore, to do what you are asking, you must do the following:

rename "datafile.txt", "datafile.old";
open (NEWDATA, ">>datafile.txt") or die ('Cannot open new data');
print NEWDATA $addthistofile."\n";
open (OLDDATA, "datafile.old") or die ('Cannot open old data');
while (<OLDDATA>){
print NEWDATA $_;
}
close OLDDATA;
close NEWDATA;
unlink "datafile.old";

Dennis

$a="c323745335d3221214b364d545a362532582521254c3640504c37292f493759214b3635554c3040606a0",print unpack"u*",pack "h*",$a,"\n\n"


Xentic
stranger

Nov 6, 2001, 5:28 AM

Post #4 of 6 (925 views)
Re: Writing form data to txt file... [In reply to] Can't Post

HI! Thanks guys!

Dennis, sorry for mailing the script to you. The script you gave me didn't work (internal server error). Just to make sure i'm doing the right thing i'm going to add a bigger part of the script to this message. This is in the top of the file:

$datafile = "../newsite/html/nieuws.txt";

And again this is the code to write it into the file:

#open your file for writing, write to it then close file
die ("Cannot open your datafile\n") unless
open (FILE, ">>$datafile");
print FILE "$addthistofile\n";
close(FILE);

But when i replace the last code with the code from Dennis I get the error.

I hope anyone can help me out!

Thanks!

Michiel



Xentic
stranger

Nov 7, 2001, 12:05 AM

Post #5 of 6 (920 views)
Re: Writing form data to txt file... [In reply to] Can't Post

HI!
I know it's not cool to reply to my own post, but i have a problem. The site i'm working on have to be ready tomorrow and i still can't get the script to work.

I can post the entire script if you want? Does anyone know what i'm doing wrong?

I hope so!!!

THANKS!

Michiel



Xentic
stranger

Nov 7, 2001, 12:27 AM

Post #6 of 6 (919 views)
Re: Writing form data to txt file... [In reply to] Can't Post

HI! This is the script. Who knows i can add the script Dennis told me to use? Any help would be apreciated a lot!

#!/usr/bin/perl

$pass = "My Password";
$allow_html = 1; # 1 = Yes; 0 = No
$datafile = "../newsite/html/nieuws.txt";
$scriptlocation = "../cgi-bin/add2file.pl";

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

@pairs = split(/&/, $input);

foreach $pair (@pairs) {

($name, $value) = split(/=/, $pair);

$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ s///g;

if ($allow_html != 1) {
$value =~ s/<([^>]|\n)*>//g;
}

$FORM{$name} = $value;
}

$password = $FORM{'password'};
$zach = $FORM{'zach'};
$addthistofile = $FORM{'addthistofile'};

unless ($password eq "$pass") {

print "Content-type: text/html\n\n";
print "<HTML><HEAD><TITLE>Add2File</TITLE></HEAD>\n";
print "<BODY BGCOLOR=BLACK TEXT=WHITE><CENTER><H1>Add2File</H1><BR>\n";
print "<FORM ACTION=\"$scriptlocation\" METHOD=\"POST\">\n";
print "Please submit the admin password to use Add2File\.\n";
print "<BR><INPUT TYPE=TEXT SIZE=10 NAME=\"password\">\n";
print "<INPUT TYPE=SUBMIT VALUE=\"Submit your Password\">\n";
print "</FORM></CENTER></BODY></HTML>\n";
exit;
}

#######################################################################
####Generate the configuration form page###############################
#######################################################################

unless ($zach eq "makechanges") {
print "Content-type: text/html\n\n";
print "<HTML><HEAD><TITLE>Add2File</TITLE></HEAD>\n";
print "<BODY BGCOLOR=BLACK TEXT=WHITE><CENTER><H1>Add2File</H1><BR>\n";
print "<FORM ACTION=\"$scriptlocation\" METHOD=\"POST\">\n";
print "In the text box below, enter what you need to add to your file\.\n";
print "<BR><textarea SIZE=40 NAME=\"addthistofile\">

<b>dd-mm-jaar</b>
</textarea>\n";
print "<INPUT TYPE=HIDDEN NAME=\"password\" VALUE=\"$pass\">\n";
print "<INPUT TYPE=HIDDEN NAME=\"zach\" VALUE=\"makechanges\">\n";
print "<INPUT TYPE=SUBMIT VALUE=\"Add2File\">\n";
print "</FORM></CENTER></BODY></HTML>\n";
exit;
}

#######################################################################
#### Write text to file ###############################################
#######################################################################

#open your file for writing, write to it then close file
die ("Cannot open your datafile\n") unless
open (FILE, ">>$datafile");
print FILE "$addthistofile\n";
close(FILE);

#######################################################################
#### Print Finish message #############################################
#######################################################################

print "Content-type: text/html\n\n";
print "<HTML><HEAD><TITLE>Add2File</TITLE></HEAD>\n";
print "<BODY BGCOLOR=Black TEXT=WHITE><CENTER><H1>Add2File</H1><BR>\n";
print "Ok\, your file has been updated\.\n";
print "Thanks for using Add2File\n";
print "</BODY></HTML>\n";
exit;


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives