Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Slightly OT: Perl and CGI - reading a file

by C_T (Scribe)
on Mar 30, 2004 at 23:37 UTC ( [id://341140]=perlquestion: print w/replies, xml ) Need Help??

C_T has asked for the wisdom of the Perl Monks concerning the following question:

In my recent Perl class we constructed a Perl/CGI script that read a list of produce items from a text file, then used those items to make a web order form. This code ran perfectly on the class Unix server.

Now I've returned home and I uploaded the same script and the same text file to my website's server. Now when I run the script it fails with an error:

Error opening produce_items.txt! : No such file or directory at /html/cgi/eproduce.cgi line 24.

First of all, here's the code I'm using:

#===== Open the file containing our list of produce $filename = "produce_items.txt"; open(PRODUCE, $filename) || die "Error opening $filename! : $!\n ";

Someone suggested trying the full pathname, so I amended the code as follows:

#===== Open the file containing our list of produce $current_directory = $ENV{PWD}; $filename = "$current_directory" . "/" . "produce_items.txt"; open(PRODUCE, $filename) || die "Error opening $filename! : $!\n ";

This fails with the same error (well, slightly different... it says "/produce_items.txt" instead of "produce_items.txt").

The text file is in the same directory as the cgi file, by the way.

I know this is a CGI problem, not a Perl problem, but perhaps someone more experienced than I can see what I'm doing wrong?

Many thanks!

Charles Thomas
Madison, WI

Replies are listed 'Best First'.
Re: Slightly OT: Perl and CGI - reading a file
by jZed (Prior) on Mar 30, 2004 at 23:43 UTC
    Does print $ENV{PWD} print anything? If not, you might benefit from using warnings and strict in your script (you should anyway) because they'd warn you that you were trying to use an undefined variable; Try
    use Cwd; my $current_dir = cwd;

    update: and remember that the current working directory of your script is set by your web server and may not be the same as the physical directory your script is located in and also may not allow you to keep regular files in the same directory as your scripts, see your web server apps. But the Cwd will at least tell you what your working directory is.

      My header is as follows:

      #! /usr/bin/perl -w use CGI qw(:standard); use CGI::Carp ('fatalsToBrowser'); #send any errors to the browser use strict;

      I'm not sure if it prints anything. I'll do some tests and get back to you.

      Charles Thomas
      Madison, WI
      update:
      use Cwd; my $current_dir = cwd;

      This gives the same error as the other two methods. You may be on to something with your advice that files may not be allowed in the same directory as the CGI scripts. I wonder if their tech support people would know anything about that...

      Charles Thomas
      Madison, WI
Re: Slightly OT: Perl and CGI - reading a file
by borisz (Canon) on Mar 30, 2004 at 23:43 UTC
    Looks like $ENV{PWD} is undefined. Try to replace $current_directory with the real path.
    Boris
      Sorry for the dumb question, but this is all new to me.

      How do I find the "real path"? This is on a paid service's webserver (i.e., commercial hosting site).

      I know how to get the current directory path when I'm logged into the shell on my server here, but I don't log into their webserver in the same way, I just log into www.myURLhere.com using an FTP client and upload the web pages.

      Again, I know this is a dumb question, but I'm very new to the whole CGI and Unix shell world.

      CT

      Charles Thomas
      Madison, WI

        Upload this script and run via CGI:

        #!/usr/bin/perl use Cwd; print "Content-Type: text/html\n\n"; print "<p>Perl says CWD is: ", cwd(); print "<p>System PWD says: ", `pwd`;

        cheers

        tachyon

        How do I find the "real path"? This is on a paid service's webserver (i.e., commercial hosting site).

        Some ways:

        • use Cwd; to get the current directory. Hopefully, that is the path the script is in — usually that is the case. Other people have showed you here how to use it.
        • Same effect, somewhat simpler: use File::Spec::Functions 'rel2abs'; which can be used to convert a file path to an absolute path:
          use File::SPec::Functions 'rel2abs'; my $abspath = rel2abs("produce_items.txt");
        But, to be honest, I don't expect these to work, because if opening a relative path doesn't work because the file doesn't exist, using an absolute path for the same location will most likely have the very same effect.

        So, you're better off hardcoding the absolute path to your data file.

        If that isn't a most favourable option, you can still try to find the position of the script using FindBin, and locate the data file from there:

        use FindBin; my $abspath = "$FindBin::Bin/produce_items.txt";

        Still, I believe you're best off moving your data out of your web space, to an absolute location, with a prereably a simple path, and hardcode it into your script.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://341140]
Approved by broquaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (3)
As of 2024-04-25 19:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found