PDA

View Full Version : Saving Logfile utilizing scalar variable


webdesignerX002
10-31-2003, 06:50 PM
I have tried everything and I can't seem to figure my problem out. I am trying to to save a text file using a scalar variable ($scalar). What I need the script to do is to open a different log file set the first line of the log to the scalar variable and then save the file name to that scalar variable.

Here is my script so far:
# Beginning of Script #
#! c:\Perl\bin\perl.exe -wT
use strict;
use CGI ':standard';

# Directory path that files will be uploaded into #
my $upload_dir = "/My Documents/Webpages/lawreview/logs/";

# Set the file name to be used for saving the upload infomation in #
my $datafile = "title.txt";

# Variable's to receive input from HTML Form #
my ($type, $number, $title, $file, $info, $MIMEType, $DIR_PATH, $FILE_NAME);
$type = param('Titletype');
$number = param('Title_Number');
$title = param('htmlText');
$file = param('uploadfile');
$info = uploadInfo($file);
$MIMEType = $info->{'Content-Type'};

# Retrieves Edition information from edition.txt file and stores it into an array #
open (LOGA, "<../lawreview/logs/edition.txt") || Error('open', 'file');
my @logmessage = <LOGA>;
close (LOGA);

#Grabs the first line on the logmessage array and saves it into $edition and then establishes filename scalar#
my $edition = $logmessage[0];
my $filename = "log$edition$type$number.txt";

# Opens text file to save title into, this is just to test dir and file name using scalar variables #
open (LOG, ">$upload_dir/$datafile") || Error('open', 'file');
print LOG "$title";
close (LOG) || Error('close', 'file');

# Establishes file using $filename and opens it and saves title into it #
open (LOGB, ">$upload_dir/$filename") || Error('open', 'file');
print LOGB "$title";
close (LOGB) || Error('close', 'file');

# Shows Information that was submitted #
print "The information you submitted was:";
print "<blockquote>";
print"

You Submitted an: $type";
print"

It is $type: # $number";
print"

The title of $type # $number is:</p>";
print"<blockquote>$title</blockquote>";
print"

The file that goes with that title is: $file";
print"</blockquote>";
print"

If the information isn't correct click backspace on the browser and re-submit the information";

# Sub Routine that is displayed if perl is unable to open or close a file #
sub Error {
print "Content-type: text/html\n\n";
print "The server can't $_[0] the $_[1] : $! \n";
exit;
}

# End of Script #

Thank you for those who take the time to help me with this.

TechCode
11-07-2003, 08:20 AM
So what seems to be the problem ?

What server/ perl are you using ?

I dont know i you put that
#begin of script#
in the real script, but first line needs to be the path to the perl !

I've added in autoexec.bat folowing line :

path=c:\server\bin\

and perl.exe is located inside of that bin\ dir.

So i use this as the first line on my system :

#!perl

Also try using

close file_handle;

Without () !

TechCode
11-07-2003, 08:25 AM
I've also noticed :

#! c:\Perl\bin\perl.exe -wT

i guess you wanted to disalow warning messages then just -w witrouth T !

I also must say that i've never seen anybody using that code to get form input !

Here is what i use in all my scripts :

#!perl

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

if ($ENV{'REQUEST_METHOD'} eq 'GET') {

$buffer = $ENV{'QUERY_STRING'};

}

else {

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

}

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

$Form{$name}=$value;
}

After this in $Form{"name"} you have value of the field name in the form on the page. It accepts GET and POST forms !

possum
11-07-2003, 10:43 AM
What is the exact problem?

Does it print out to the file?

becuase you are missing a print "Content-type: text/html\n\n"; before you print out your "Shows Information that was submitted"

As for the way you got the input, i have seen that before and it looks correct.

Also, the -T ensures that your program runs in without tainted data. So, remove that and i think it will work, or just do a check to make sure that your data is not tainted

How to is found at http://www.uga.edu/~ucns/wsg/unix/perl/taint.html


I hope i was able to help.

webdesignerX002
11-19-2003, 03:40 AM
Thanks for the suggestions. I had already figured it out. It was the taint mode that was causing the problem, I just sent the variable through a search and that un-tainted the variable. I was told in the past never to remove the -T because that could cause problems in the future.

As for the location of perl, sorry about that. I am testing my scripts on my computer utilizing a virtual server. This way I know everything works before I put it online. Thats why the shebang line points to perl on my c: drive.

Thanks again for helping though. Posted this on other boards and this was the only one to answer my post.