in reply to use variable as name for new file

Welcome!

The Perl CGI script is being run under the userid of the webserver (I'm assuming this is Apache under FreeBSD or Linux?), not your own. The directory where your file is to be created/opened needs to be writable by the webserver.

First, find out what userid the httpd daemons are running as:
$ ps -aux | grep httpd root 17714 0.0 0.2 5184 4456 ?? Ss 7Jun06 0:12.49 /usr/lo +cal/sbin/httpd www 17715 0.0 0.4 9164 8272 ?? I 7Jun06 0:00.24 /usr/lo +cal/sbin/httpd www 17716 0.0 0.4 8744 7864 ?? I 7Jun06 0:00.15 /usr/lo +cal/sbin/httpd www 17717 0.0 0.4 8768 7996 ?? I 7Jun06 0:00.19 /usr/lo +cal/sbin/httpd www 17724 0.0 0.4 8748 7788 ?? I 7Jun06 0:00.19 /usr/lo +cal/sbin/httpd www 17756 0.0 0.4 8772 7820 ?? I 7Jun06 0:00.20 /usr/lo +cal/sbin/httpd dwilde 7928 0.0 0.0 1512 988 p7 S+ 6:28AM 0:00.00 grep ht +tpd
You'll note that the primary (on my system) is running as root, the rest as user 'www'. Therefore, the directory on the filesystem -- and it should be fully specified, such as '/var/web/root/logs/mylog.log' -- needs to be writable by www, or whatever your server is set up as. Apache configurations are very flexible, and it's equally likely your server will be running under user 'nobody'. In any event, make writing possible:
$ su Password: # chown www /var/web/root/logs # chmod 744 /var/web/root/logs # exit $
As has been mentioned, allowing somebody to open a file on your server and write to it can cause mucho heartburn. Be very careful what you allow and who you allow to do it. I'd suggest that you force the directory and only allow the filename to be chosen:
my $subject = param('subject'); open (LOG, ">/var/web/root/logs/$subject") || die "Error opening file +: $!\n";
And make sure that your script is limited as to how much information can be written to the file. Bear in mind that Perl can be used to poke a webserver as well as being poked, and some nasty little script kiddie will surely use it to do so at some point in your life. =8^O

Don Wilde
"There's more than one level to any answer."

Replies are listed 'Best First'.
Re^2: use variable as name for new file
by Zcity (Novice) on Jun 16, 2006 at 14:40 UTC
    THis is the error Im getting

    createfile.cgi did not produce a valid header (name without value: got line "syntax error at createfile.cgi line 27, near "$subject")
      I see that you've posted more, thanks. It's not the file write at all. You should create a CGI query object and use the OO interface. If you use the functional interface there's another step to it, before you can read parameters. Forgotten what it is at the moment, sorry.
      my $q = new CGI; my $subject = $q->param('subject');
      Is the page this is run from _sending_ the parameters??? This might be your real problem. Is your <form> set up properly?

      Don Wilde
      "There's more than one level to any answer."
        You should create a CGI query object and use the OO interface.

        Why? The functional interface works fine here. What advantages do you think the OO interface has?

        If you use the functional interface there's another step to it, before you can read parameters. Forgotten what it is at the moment, sorry.

        To use the functional interface you need to import the required functions into your program's namespace. One way to do that is with the

        use CGI ':standard';

        line that this program already has. So it seems that the functional/OO difference is a complete red herring here.

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

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