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

Hi,

Is there a special way to create a file with cgi script.
I tried this with system command:
# within some_script.cgi # Store information of the user system("cat $par1 > ~/public_html/cgi-bin/user_log $par2");
But there is no file get created within "/usr_log".

Replies are listed 'Best First'.
Re: creating a file with a cgi script
by davorg (Chancellor) on May 19, 2006 at 07:56 UTC

    I'd guess that the web server user doesn't have rights to write to that directory. You don't seem to be doing any kind of error checking.

    And that's not a very portable or efficient way to write to a file. Why not use "open", "flock" and "print"?

    Two other small points. Firstly, writing log files in your cgi-bin isn't a very good idea. Depending on how your web server is configured, it's possible that the server will try to execute them when they are requested by a browser. Secondly, I'm not sure what is in you $par2 variable, but the syntax of your "cat" call doesn't look right.

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      Hi davorg,
      Following your advice I tried this
      my $OUTFILE_file_name = "../user_log/".$req_id."\.txt"; open ( OUTFILE, '>', $OUTFILE_file_name ) or die "$0 : failed to open output file $OUTFILE_file_name : $ +!\n"; print OUTFILE $uemail; close ( OUTFILE ); # close output file
      But it still doesn't get printed in user_log/
      And I also get this message
      failed to open output file ../user_log/22410.txt : Permission denied
      What's wrong with it?

        As I said before, your web server process doesn't have permission to write to that directory. The error message seems pretty clear to me, but if you want more detail then try adding "use diagnostics" to your program.

        Which user does your web server run as? Do you use suexec at at? What are the permissions on the directory that you are trying to write to?

        --
        <http://dave.org.uk>

        "The first rule of Perl club is you do not talk about Perl club."
        -- Chip Salzenberg

        You're trying to write in your ${HOME}, but as apache is running as a specific user (www or apache), it can't perform the operation, as it doesn't have the proper permissions for doing that.

        Igor 'izut' Sutton
        your code, your rules.

Re: creating a file with a cgi script
by leocharre (Priest) on May 19, 2006 at 13:40 UTC

    As someone commented before- this is a cgi, right? So.. it's being run by a http trigger, so.. it's run by apache.. Unless you have suexec running, if you look at the process, it would be running under wwwrun, or some other 'user' . Even though the script may be owned by you.

    So, when you ask for $ENV{HOME} (if it is set) or ~, you are asking for the home of some other user. Likely.

    If yuo're on a *ix box, you're lucky or smart; so why don't you try this.. print STDERR $ENV{HOME};

    Then in your shell prompt, do this ..  tail -f /var/log/httpd/error_log - that will let you see STDERR as you go (leave that window runninng, and as you try out your script, the window will output your cgi STDERR). *your* exact apache error log may be somewhere else, like /var/log/apache2/error_log .

    That will help debugging