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

I have been playing with this for about an hour and can't seem to do it.

Is there a way to create a file from a cgi script. I can't get the script to create a file. though I can edit an existing file. It might be a permissions thing.
Security is not really an issue as it's for an internal machine with an unroutable IP. What I'm trying to create is a simple web based HTML editor.

Your humble servant
-Chuck

Replies are listed 'Best First'.
Re: Creating a file in a CGI script...
by httptech (Chaplain) on May 19, 2000 at 21:03 UTC
    The webserver probably doesn't have permission to write to the directory where you want to create the file. Try chmod 777 on the directory in question.
Re: Creating a file in a CGI script...
by perlcgi (Hermit) on May 20, 2000 at 13:30 UTC
    What's sometimes helpful to debug this type of problem is to monitor the server logs as you use the script. So if its a Unix box and Apache do a tail -f /usr/local/apache/logs/access.log or similiar. If its a busy production server, (maybe you should'nt be using it for devel work), you can always grep for your script.
    On NT, if God-help-us, you are using IIS, then logfiles default to \Winnt\system32\LogFiles\W3SVC3\
      Of course even simpler than monitoring the logs file is to use CGI::carp
      From the CGI::Carp doc:
      By default, error messages are sent to STDERR. Most HTTPD servers direct STDERR to the server's error log. Some applications may wish to keep private error logs, distinct from the server's error log, or they may wish to direct error messages to STDOUT so that the browser will receive them.
        Just put the following line near the start of your file, and you'll receive fatal error messages (from die -- you are using die, aren't you?) in your web browser. No need to tail -f the logs with: use CGI::Carp qw(fatalsToBrowser);
Re: Creating a file in a CGI script...
by Tibetan_Monk (Initiate) on May 20, 2000 at 00:36 UTC
    You can do
    open IN, ">filename";
    it will automatically create the file and you can write to it at will. if you create your file before-hand... use chmod 777 filename
Re: Creating a file in a CGI script...
by ChuckularOne (Prior) on May 19, 2000 at 21:11 UTC
    Thanks princepawn, Thanks httptech.

    Your Humble Servant,
    -Chuck
      Wow. I was just about to post the same question. Except my situation is a little different. My script is going to be hosted on my ISPs NT/IIS/Perl box.. unfortunately. I can read from a file, but can't write. It's a file that exists too... but that doesn't seem to matter. CHMOD doesn't work either. Apparantly it isn't supported .... and hints? Thanks much, JoeG -
        Check out what open says when you try to open the file in write mode and (presumably) fail. Check the value of $!, check the error logs for the site, try writing a file in another location (e.g. /tmp or equivalent), and find out what user you are running as and the permission it has for each directory.
RE: Creating a file in a CGI script...
by LeGo (Chaplain) on Oct 04, 2000 at 08:38 UTC
    I just finished doing a cgi script (well a part of it). I realized that you might want to make sure the file exist or doesn't exist. Depending on whether or not you want it to. I did not want the file to exist so I did this.
    my $file = "data2.dat"; if (-e $file){ $file = "data3.dat"; open (FILE, ">$file") or die "Can't open the file $!\n";} else { open (FILE, ">>$file") or die "Can't open the file $!\n";}
    Basically if the file exist I wanted to create another file. This was so I did not erase anything.

    LeGo

RE: Creating a file in a CGI script...
by Anonymous Monk on May 19, 2000 at 21:49 UTC
    ooops.

    Maybe a course in Unix basics would've helped...

    You've got to remember that (with the exception of the setuid option in apache that isn't enabled by default), apache will run scripts with the permissions of the user it is running at. If it's installed correctly, it will be running as an unprivilaged user (perhaps the nobody user, or a specific user for the server). So you have to setup directory permissions as appropriate. To have the ability to create new files, you must have write permission of the directory. This is because you are changing the directory, effectively, whereas writing to, or over a file already in existance, requires just write permission on that file.

Re: Creating a file in a CGI script...
by princepawn (Parson) on May 19, 2000 at 21:08 UTC
    I have had this problem before. In our case, the apache configuration file was setup to prohibit any writes to certain directories... I wrote the file to /tmp and it worked
      Hmm, I don't think Apache can control what directories you can write to from your Perl script, since Perl is writing directly to the file, not through the http server. You fixed your problem by moving to a directory where the webserver user had system permissions to write to.