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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read POST data and write to a text file

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
0
0
I am unfamiliar with Perl and I could use some help here. I have written a Java Applet that needs to save data to a text file. However, Java can't write to a file because of security issues. So, I used Java to POST the data to a cgi script. The data is formatted exactly as it needs to be in the final text file.

Basically, I need this CGI Perl script to put the whole chunk of data in a text file. Can anyone give me some tips on how to go about this?

Thanks
 
You will need the form data field name which the text is being passed to. Then you must have a parsing routine to prep the data, then after parsed, retrieve the data, and store it to a variable. Open a text log file and write it to the file. Close the file, then print some output.
>> will add to data that is already in the log file, > will erase whatever is in the file being written to and replace with the new. I have created entire database with flat text files so I know this.

Heres the code, the parse routine is from the Visual quickstart guide which I store in a separate file named main.lib

create a cgi-file named write_text.cgi
Be sure that owner permission of write_text.cgi is set to execute or you will get an error.

write_text.cgi

#!/usr/bin/perl

require "../library/one.lib";
#Uses library where parse routine resides ../moves up one directory

&parse;
#parsing data subroutine

&mime;
#html output subroutine

$data_to_write = $formdata {'java_data'};
#pull data from form field sent by java, java_data is the variable containing java text to be written

open (DATALOG, ">>../log.text")|| &errormes;
#open logfile for access

flock (DATALOG, 2);
#creates exclusive access to file

print DATALOG "$data_to_write";
#prints form data to file

flock (DATALOG, 8);
#release exclusive access of file

close (DATALOG);
#closes filehand

#print output for user to let know function performed successfully
print &quot;<center>&quot;;
print &quot;<hr size=&quot;100%&quot; color=&quot;#990000&quot;;
print &quot;DATA WRITTEN TO TEXT FILE SUCCESSFULLY!!&quot;;
print &quot;<hr size=&quot;100%&quot; color=&quot;#990000&quot;;
#END OF write_text.cgi

Here is the library file which you also need to create to parse your data to get it ready to print to the text file

Here is the code for your library file main.lib

sub parse {

if ($ENV{'REQUEST_METHOD'} eq 'GET') {
@pairs = split(/&/, $ENV{'QUERY_STRING'});
} elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
read (STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
} else {
print &quot;Content-type: text/html\n\n&quot;;
print &quot;<P>Use Post or Get&quot;;
}

foreach $pair (@pairs) {
($key, $value) = split (/=/, $pair);
$key =~ tr/+/ /;
$key =~ s/%([a-fA-F0-9] [a-fA-F0-9])/pack(&quot;C&quot;, hex($1))/eg;
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9] [a-fA-F0-9])/pack(&quot;C&quot;, hex($1))/eg;

$value =~s/<!--(.|\n)*-->//g;

if ($formdata{$key}) {
$formdata{$key} .= &quot;, $value&quot;;
} else {
$formdata{$key} = $value;

}
}
}


sub errormes {
print &quot;<hr width=100% color=#000099 size=12 style='border:7px ridge #000099'>&quot;;
print &quot;<HTML><HEAD><TITLE>error message</TITLE></HEAD><BODY BGCOLOR=#FFFFFF><CENTER><BR><BR>&quot;;
print &quot;<TABLE BORDER=1 WIDTH=416 CELLSPACING=0>&quot;;
print &quot;<TR><TD BGCOLOR=#000099 align=center>&quot;;
print &quot;<FONT SIZE=5 COLOR=black<B>Unable to open or write to file</B></FONT></TD></TR></TABLE></BODY></HTML>&quot;;
exit;
}
sub mime {
print &quot;Content-type: text/html\n\n&quot;;
}
1; #If return value is not set, library will not work!!

<i>Its okay to dream.....</i>
 
tmtowtdi ;-)
Code:
#!/usr/local/bin/perl 
use strict;
use CGI;
use CGI::Carp 'fatalsToBrowser';

# instantiate a CGI object
my $obj = new CGI;

# start some basic HTML output
print $obj->header,
      $obj->start_html,
      qq(<ul>);
      
my $f_lock = '2';
my $f_unlock = '8';

# open a file to write to
open(OUTPUTFILE,&quot;>>junk.txt&quot;) or 
        die &quot;Failed to open OPF, $!\n&quot;;

# get an exclusive lock on that file
flock OUTPUTFILE, $f_lock;

# seek to the end of the existing content
# incase a previous process wrote to the file
# after the open and now
seek OUTPTUFILE, 0, 2;

# get an array of the passed CGI parms
my @params = $obj->param;

# get a value for each parm and print them
foreach my $key (@params) 
	{
	my $value = $obj->param($key);
	print &quot;<li>$key : $value&quot;;
	print OUTPUTFILE &quot;$key: $value\n&quot;;
	}

# unlock the file	
flock OUTPUTFILE, $f_unlock;

# gracefully close your HTML
print qq(</ul>), $obj->end_html;
'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
Can you tell me why this won't work.

#!Perl/bin/perl
#savecalendar.cgi

use CGI;

my $q = new CGI;

$data_length = $ENV{'CONTENT_LENGTH'};
$bytes_read = read(STDIN, $my_data, $data_length);

open(fileOUT, &quot;>/Calendar/calendar.txt&quot;) or die &quot;cannot open file: $!&quot;;
flock(fileOUT, 2) or die &quot;cannot lock file exclusively: $!&quot;;

print fileOUT $my_data;
close(fileOUT);

# END
 
Code:
#!/usr/local/bin/perl
print &quot;Content-type: text/html\n\n&quot;;
if ($ENV{'REQUEST_METHOD'} eq &quot;POST&quot;) 
    {
    read(STDIN, $my_data, $ENV{'CONTENT_LENGTH'});
    }
elsif ($ENV{'REQUEST_METHOD'} eq &quot;GET&quot;) 
    {
    $my_data = $ENV{'QUERY_STRING'};
    }

open(fileOUT, &quot;>>./try_me.txt&quot;) or
      die &quot;cannot open file: $!&quot;;
flock(fileOUT, 2) or 
      die &quot;cannot lock file exclusively: $!&quot;;

print qq(<pre>Data: $my_data</pre>);
print fileOUT $my_data;
close(fileOUT);
[code]

------------------------------------------------------------
[b]First[/b], your web server expects your CGI code to output a compliant http header.  Your code is not outputing anything.  Even if your java applet does nothing with the returned stuff, the web server is going to expect your CGI to send it.  So, include a 'Content-type:....' line in your code.  If you don't, you'll get a web server error like, &quot;premature end of script headers&quot; or something similar.

[b]Second[/b], You are mixing two approaches to writing CGI:
1 - the verbose way (no CGI.pm) and
2 - the CGI.pm way

Since you are not using any of the CGI.pm methods, you don't need to load it nor do you need to instantiate the CGI object.  So, you can leave these lines out.
[code]
use CGI;
my $q = new CGI;

You can do the same stuff with CGI.pm, but I don't see the need in this situation.

Third, I'm not sure what request method you are using,
so I wrote the above stuff to account for a POST or a GET.

Fourth, ANY CGI represents a security risk, if you expand this much, be careful not to leave your self vulnerable. 'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top