Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

CGI file writing

by mcogan1966 (Monk)
on Oct 29, 2003 at 13:30 UTC ( #302992=perlquestion: print w/replies, xml ) Need Help??
mcogan1966 has asked for the wisdom of the Perl Monks concerning the following question:

I have a CGI script running from
where blah is the WWW-Root directory. Inside the script I have the following lines...
open LOGFILE, ">>search_log"; print LOGFILE "stuff\n"; close LOGFILE;
Problem is, I can't figure out where search_log is. Or even where the file should be in case I need to initialize it's existance.

I should know this, but for some reason, I'm having vapor lock on this. Where is search_log?

Replies are listed 'Best First'.
Re: CGI file writing
by jeffa (Bishop) on Oct 29, 2003 at 13:41 UTC
    Check out Cwd, you should be able to print out the value of Cwd::cwd() and determine where you are:
    use Cwd; warn cwd(),"\n";
    Once you do find the path, you should add it to your program:
    open LOGILE, '>', '/full/path/to/search_log' or die $!;


    (the triplet paradiddle with high-hat)
      Original text removed due to programmer error.

      I goofed, and forgot to remove the # from the beginning of the line. My bad, worked fine. Thanks.

      But my concern is that I now have to have a hard-coded directory for this. This code is going to be moved to a different server for production, so I either have to change the directory, or find a different solution.

        I don't quite understand - you are writing the output of a cgi script to a log file. Do you understand that the webserver usually already has a STDERR log file - the Apache webserver has a log file at /var/log/httpd/error_log and when your perl cgi script writes to STDERR, those log messages appear in that error_log file.

        Assuming you already understand that, and you still want to write to a separate log file from your cgi script, then you either have to hardcode the absolute log filename (including the path), or you could use the DOCUMENT_ROOT environment variable to base the log file location on. For example:

        my $logfile = qq!$ENV{"DOCUMENT_ROOT"}/my_logfile!; open(LOG, ">>$logfile") || die "Can't open $logfile: $!"; print LOG "Some message from my CGI script!\n";

        But the danger there is that your log file will be accessible under the document root - so if your webserver gets compromised, your log file could be read by the attacker.

        I would recommend either having your cgi script write to STDERR so that the message will appear in the standard webserver error log (outside the document root), or hardcode the location of the log file in your script, or put the location of the logfile in a site module that gets "use"d by your cgi script on both your dev server and your prod server.

        One other option if you want to keep your log file separate from the standard webserver error log, is to use the environment variable HTTP_HOST or SERVER_NAME to construct an if test around which log file name(and path) to use.

        Hi mcogan1966,
        How I normally get around this is either create a module .pm which can be included in any dir in @INC and called with use Foo::Bar;. Or a library .pl called with require /path/to/;.

        You would then make filenames and paths variables which are read in from the file making it portable throughout many systems without having to touch the script itself.

Re: CGI file writing
by bart (Canon) on Oct 29, 2003 at 15:19 UTC
    Are you sure it even exists? You don't check the success of the open call. It could be you're writing into void space.

    Otherwise, I'd expect it to be next to the script itself. That usually is a place where you're not allowed to create files by script. If you create the logfile by hand, and set its permissions to writable by anybody, you should be able to write to it.

    But in general, like jeffa wrote, the function cwd() from the module Cwd (I think it's a core module) should be able to tell you where the file is supposed to appear.

Re: CGI file writing
by jdtoronto (Prior) on Oct 29, 2003 at 14:03 UTC
    Try this (untested)
    my $cgidir = '.'; my $logfile = 'search_log'; my $filename = $cgidir.'/'.$logfile;
    It should put the log file in cgi-bin


    Updated corrected missing slash between path and filename. Thanks bart

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://302992]
Approved by valdez
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2017-01-16 23:11 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (151 votes). Check out past polls.