On Nov 21, 2003, at 2:03 AM, Gohaku wrote:

Hi everyone,
I am trying to write to a file when running the following Perl/CGI script:
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print "Hi";
open(FILE,">hello.txt") || die("Cannot Open File");
print FILE "Hello";


I have run the script from the command line and "hello.txt" does appear but if I run the script from a Web Browser, "hello.txt" does not get created.

This is a *very* frequently asked question. Take a look at the Perl FAQ. It's on your machine already, as a man page - see 'man perlfaq9'. Or, view it online at <http://www.perldoc.com/perl5.8.0/pod/perlfaq9.html>. There's also the "Troubleshooting Perl CGI Scripts" at <http://www.perl.org/troubleshooting_CGI.html";> Having said that...


I'm assuming that the script is executable, since you've indicated it runs OK from a shell prompt. Aside from that, there are two issues that immediately come to mind with the script as written: The path to the file you're writing to, and permissions.

First of all, you need to fully specify the location of the file you want to write to. That is, instead of just supplying the file name, you need to give the directory as well. For example, you might want to use "/tmp/hello.txt" instead of just "hello.txt".

Also, the file must be writable by the user and/or group the web server is running as. On Mac OS X, the user and group are both "www". To append to an existing file, the file must be writable; to create a new file, the directory in which the file will be created must be writable. One of the best ways to do this is to change the ownership of the file/directory to "www". That allows the web server to write to the file, without having to make the file world-writable.

When I tried "use CGI::Carp qw(fatalsToBrowser)

An excellent idea. The standard "505 - Server Error" is arguably the most confusing, least useful error message ever written in the history of programming.


But, the die() statement in the script above only tells you it couldn't open the file. That's good, but it could be better - it could also tell you *why* it couldn't open the file. The $! variable contains an explanation of the latest error. You could include this in your die() message, like die("Error opening hello.txt: $!"). Or, you could simply call die() with no message, which just prints $! by itself.

I also ran the script from a browser as Root and I still got the same message.

I hope I'm misunderstanding you... Are you saying you changed the "User" parameter in httpd.conf to run the server as root? I hope not - running Apache as root is a huge, major, gaping security problem. You should never do that.


sherm--

Reply via email to