For Programmers: Free Programming Magazines  


Home > Archive > PERL CGI Beginners > April 2005 > PATH problem









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author PATH problem
TapasranjanMohapatra

2005-04-25, 3:55 pm


All,
My script goes like this...
--------------------------------------
#!/usr/bin/perl
print "Content-type: text/html\n\n";
$cmd =3D "cat file_one";
$content =3D qx!$cmd!;
print "$content";
--------------------------------------
I have a case where file_one is not in the same directory. So I give the =
absolute path of file_one
in place of file_one.=20
When run it using perl on command line I get the contents of file =
printed correctly.

But when accessed through browser (cgi-bin), I get nothing printed.

I thought it might be path problem , but it is not. Because when the =
file is in same directory,
it prints the content of the file.

Can anybody let me know where I am going wrong?
TIA
tapas
Sean Davis

2005-04-25, 3:55 pm

Not sure this is the problem, but remember that when you run a CGI
script from the command line, you are running as yourself. When you
run it in the browser, you are running under whatever username and
group the server is running with (and almost always this is different
from yourself). So, you have to be sure that the directory that you
want to read from can be read by the user under which the browser runs.
So, it isn't a PATH problem, but a permissions problem.

Sean

On Apr 25, 2005, at 7:17 AM, TapasranjanMohapatra wrote:

>
> All,
> My script goes like this...
> --------------------------------------
> #!/usr/bin/perl
> print "Content-type: text/html\n\n";
> $cmd = "cat file_one";
> $content = qx!$cmd!;
> print "$content";
> --------------------------------------
> I have a case where file_one is not in the same directory. So I give
> the absolute path of file_one
> in place of file_one.
> When run it using perl on command line I get the contents of file
> printed correctly.
>
> But when accessed through browser (cgi-bin), I get nothing printed.
>
> I thought it might be path problem , but it is not. Because when the
> file is in same directory,
> it prints the content of the file.
>
> Can anybody let me know where I am going wrong?
> TIA
> tapas
>
> --
> To unsubscribe, e-mail: beginners-cgi-unsubscribe@perl.org
> For additional commands, e-mail: beginners-cgi-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>


Wiggins d'Anconia

2005-04-25, 3:55 pm

TapasranjanMohapatra wrote:
> All,
> My script goes like this...
> --------------------------------------
> #!/usr/bin/perl
> print "Content-type: text/html\n\n";
> $cmd = "cat file_one";
> $content = qx!$cmd!;
> print "$content";
> --------------------------------------


You should not shell out to read a file, Perl is a full programming
language, you should use the builtin functions whenever possible.
Especially when they are common and simple ones. It is faster, safer,
and less bug/error prone.

#!/usr/bin/perl
use strict; # always
use warnings; # pretty much always

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

open my $HANDLE, 'file_one' or die "Can't open file for reading: $!";
while (my $line = <$HANDLE> ) {
print $line;
}
close $HANDLE;

The same thing only faster, much safer, and it will give you diagnostic
output in the error log of the web server to find out why it can't read
a particular file. See the other poster's comments too.

http://danconia.org

> I have a case where file_one is not in the same directory. So I give the absolute path of file_one
> in place of file_one.
> When run it using perl on command line I get the contents of file printed correctly.
>
> But when accessed through browser (cgi-bin), I get nothing printed.
>
> I thought it might be path problem , but it is not. Because when the file is in same directory,
> it prints the content of the file.
>
> Can anybody let me know where I am going wrong?
> TIA
> tapas
>

Lawrence Statton

2005-04-25, 3:55 pm

>
> All,
> My script goes like this...
> --------------------------------------
> #!/usr/bin/perl
> print "Content-type: text/html\n\n";
> $cmd = "cat file_one";
> $content = qx!$cmd!;
> print "$content";
> --------------------------------------
> I have a case where file_one is not in the same directory. So I give the abso
> lute path of file_one
> in place of file_one.
> When run it using perl on command line I get the contents of file printed cor
> rectly.
>
> But when accessed through browser (cgi-bin), I get nothing printed.
>
> I thought it might be path problem , but it is not. Because when the file is
> in same directory,
> it prints the content of the file.
>
> Can anybody let me know where I am going wrong?
> TIA
> tapas
>


First: Check permissions. Can the webserver process (probably running
as a de-privileged user) read that file?

Second: Is your webserver running in a chroot(2)ed environment, such that
/path/to/file_one is not valid from within its context?

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Lawrence Statton - lawrenabae@abaluon.abaom s/aba/c/g
Computer software consists of only two components: ones and
zeros, in roughly equal proportions. All that is required is to
sort them into the correct order.





TapasranjanMohapatra

2005-04-26, 8:55 am




-----Original Message-----
From: Lawrence Statton [mailto:lawrence@cluon.com]
Sent: Mon 4/25/2005 8:19 PM
To: TapasranjanMohapatra
Cc: beginners-cgi@perl.org
Subject: Re: PATH problem=20
=20
>=20
> All,
> My script goes like this...
> --------------------------------------
> #!/usr/bin/perl
> print "Content-type: text/html\n\n";
> $cmd =3D "cat file_one";
> $content =3D qx!$cmd!;
> print "$content";
> --------------------------------------
> I have a case where file_one is not in the same directory. So I give =

the abso
> lute path of file_one
> in place of file_one.=20
> When run it using perl on command line I get the contents of file =

printed cor
> rectly.
>=20
> But when accessed through browser (cgi-bin), I get nothing printed.
>=20
> I thought it might be path problem , but it is not. Because when the =

file is=20
> in same directory,
> it prints the content of the file.
>=20
> Can anybody let me know where I am going wrong?
> TIA
> tapas
>=20


First: Check permissions. Can the webserver process (probably running
as a de-privileged user) read that file?

Second: Is your webserver running in a chroot(2)ed environment, such =
that
/path/to/file_one is not valid from within its context?

Could you give me some idea how to know if it is chroot(ed). Because the =
files have al the permissions for everyone (777).
Thanks

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --=20
Lawrence Statton - lawrenabae@abaluon.abaom s/aba/c/g
Computer software consists of only two components: ones and
zeros, in roughly equal proportions. All that is required is to
sort them into the correct order.






Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com