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

Hi, I am trying to run a CGI script but my program dies on this line:
open(IMAGE, ">/somedir/$graphfile.png") or die "$_";
I think it is because of the file permissions. When I run my script from the command line I get no problems. When I run my script from the browser the program dies and says permission is denied. Is there a way to change file permissions for files created within a script? If not, what might be the cause of death?

Replies are listed 'Best First'.
Re: Changing file permissions in a script
by kyle (Abbot) on Oct 08, 2008 at 20:16 UTC

    The user that runs the CGI under the web server doesn't have permission to write files to /somedir/. If you don't mind having it globally accessible to everyone, you can set the permissions this way:

    chmod 777 /somedir/
      How do I change the user that CGI runs under?

        How do I change the user that CGI runs under?

        That's a matter of web server configuration. You haven't mentioned which web server you're using, but the documentation for it can probably tell you how to do that.

Re: Changing file permissions in a script
by Your Mother (Archbishop) on Oct 08, 2008 at 22:47 UTC

    And this surely isn't right-

    open(IMAGE, ">/somedir/$graphfile.png") or die "$_";

    Try-

    open(IMAGE, ">/somedir/$graphfile.png") or die "Couldn't open /somedir/$graphfile.png for writing: $!";