help! trouble reading / writing files in IIS

  • Thread starter Mad Scientist Jr
  • Start date

M

Mad Scientist Jr

I am having trouble running some active perl in IIS5 under Win XP Pro.

I have activeperl installed, and perlscript is running - I got a
perlscript asp page to read a textbox from a form and do some simple
string comparisons, but am having trouble reading & writing files.

I have a file c:\inetpub\wwwroot\perl_read_file.txt that contains the
following text:

<h1>success</h1>
<p>read the file</p>

the script attempts to read.
The script also attemps to write to a file "perl_write_file.txt" in
wwwroot

The script is saved in c:\inetpub\wwwroot\perl_read_file.asp
and accessed at http://localhost/perl_read_file.asp

First I thought it might be permissions, but I have given the system
user ASP.NET full permissions to my c:\inetpub\wwwroot directory.
Unless there is some other user that the active perl stuff uses?

Here is the code (you will see many versions commented out, I have
tried different approaches)

Any help appreciated

<%@Language=PerlScript%>

<%

#
-----------------------------------------------------------------------------
# SET DEFAULTS
$sPage = "perl_read_file.asp";

#
-----------------------------------------------------------------------------
# GET VARIALBES FROM FORM
my($sFile) = $Request->Form('txtFile')->Item();

#
-----------------------------------------------------------------------------
# TEST FOR INPUT
if ($sFile eq '')
{
$sMessage = "Please type a file name and click [Submit].";
$sDefault = "perl_read_file.txt";
}
else
{
# $sMessage = "READ FILE: <br>";
#
# while ($myFile = <$sFile>)
# {
# open (FILE_IN, "$myFile") ;
# while ( $sNextLine=<FILE_IN> ) # read file one line at a time,
until end
# {
# $sMessage = join $sMessage, "\n", $sNextLine;
# }
# close (FILE_IN);
# }
#
# #my $filename = $Server->MapPath('/$sFile');
# $filename = $Server->MapPath('/$sFile');
#
# open (MYFILE, '$filename');
# while (<MYFILE>)
# {
# chomp;
# #print "$_\n";
# $sMessage = join $sMessage, $_, "\n";
# }
# close (MYFILE);
#
# #open( FILE, $Server->MapPath('/scripts/forum.asp') );
# #$sFile = join $Server->MapPath('/$sPage'), $sFile
# #$sFile = $Server->MapPath('$sPage')
#
# $sDefault = "$sFile";
# my $path = $Server->MapPath($Request->ServerVariables('PATH_INFO'));
# $sMessage = join $sMessage, "\n", $spath;
#my $path = $Server->MapPath($Request->ServerVariables('PATH_INFO'));
my $path = $Server->MapPath($sFile);
#$sMessage = $path;
open (MYFILE, '$path');
while (<MYFILE>)
{
chomp;
#$sMessage = join $sMessage, $_, "\n";
$sMessage = $sMessage . "x";
}
close (MYFILE);

} # else

#
-----------------------------------------------------------------------------
# WRITE A FILE
my $path = $Server->MapPath('perl_write_file.txt');
open (MYFILE, '>>$path');
print MYFILE "user requested $sFile\n";
close (MYFILE);

#
-----------------------------------------------------------------------------
# ENCODE OUTPUT
#$sMessage = $Server->HTMLEncode($sMessage);

%>

<HTML>
<TITLE>PerlScript Test Read File</TITLE>
<BODY>
<FORM ACTION="<% $Response->Write("$sPage"); %>" METHOD="POST">

<P>
<% $Response->Write("$sMessage"); %>
</P>
<TABLE>
<TR>
<TD align="right">File:</TD>
<TD align="left">
<INPUT NAME="txtFile" VALUE="<% $Response->Write("$sDefault");
%>">
<INPUT TYPE="SUBMIT" VALUE="Submit">
</TD>
</TR>
</TABLE>

</FORM>
</BODY>
</HTML>
 
Ad

Advertisements

M

Matt Garrish

Mad Scientist Jr said:
I am having trouble running some active perl in IIS5 under Win XP Pro.

I have activeperl installed, and perlscript is running - I got a
perlscript asp page to read a textbox from a form and do some simple
string comparisons, but am having trouble reading & writing files.

I have a file c:\inetpub\wwwroot\perl_read_file.txt that contains the
following text:

<h1>success</h1>
<p>read the file</p>

the script attempts to read.
The script also attemps to write to a file "perl_write_file.txt" in
wwwroot

The script is saved in c:\inetpub\wwwroot\perl_read_file.asp
and accessed at http://localhost/perl_read_file.asp

First I thought it might be permissions, but I have given the system
user ASP.NET full permissions to my c:\inetpub\wwwroot directory.
Unless there is some other user that the active perl stuff uses?

PerlScript will run as the IUSR. Only aspx pages run as the ASP.NET user. I
don't think that's your problem, though.
my $path = $Server->MapPath($sFile);
open (MYFILE, '$path');

You have a really big problem right here. You're single-quoting your path,
so you're effectively telling Perl to try and open a file called $path, not
the actual path contained in the variable. This is one of the reasons you
should never needlessly quote variables in perl:

open(MYFILE, $path);

Since you're in an asp page, you might want to consider wrapping whatever
processing you do in an if conditional so you can do something else if the
open fails:

if (open(my $fh, '<', $path)) {
# do something with $fh
}

else {
$Response->write('oops. better do something else!');
}

Matt
 
B

Brad Murray

MSJ> open (MYFILE, '$path');

Check the return from open -- I think you'll find it failed and why.

open (MYFILE, $path) or die "WTF?! $!";
 
T

Tad McClellan

Mad Scientist Jr said:
am having trouble reading & writing files.

First I thought it might be permissions,


If you ask Perl to tell you why, then it will tell you why,
and you won't have to make random guesses like that.

open (MYFILE, '$path');


You should always, yes *always*, check the return value from open():

open (MYFILE, $path) or die "could not open '$path' $!";


Have a look at the filename argument that you are passing to open

print('$path');

and I think you'll see another problem to fix.
 
D

Dr.Ruud

Tad McClellan schreef:
You should always, yes *always*, check the return value from open():

What would be a good way to automate that?

Maybe a variant on "use strict", like "use stricter" or "use akela".
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top