Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Cannot save to file with a CGI scipt

by chuleto1 (Beadle)
on Sep 23, 2002 at 15:24 UTC ( #200130=perlquestion: print w/ replies, xml ) Need Help??
chuleto1 has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks:
I am having problems saving some params to a file ( printing();)($file, look at the code). The funny thing is the when I run the code (>perl file.pl ) a file is created. This is what I get in the apache server error log "Cannot save file /var/www/html/datfiles/data.dat at /var/www/cgi-bin/results.pl line 48. ". Please help!
#file.pl #!/usr/bin/perl use strict; use CGI::Cookie; use CGI; my $cgi = new CGI; my $file = "/var/www/html/datfiles/data.dat"; print $cgi->header( -cookie => new CGI::Cookie(-name => 'Cho +colateChip', -value=> $cgi- +>param('color'), -expires => 'T +hu, 26-Sep-2002 00:00:00 GMT'#, #-domain => 's +tudents.byu.edu', #-path => '/' ) ); print $cgi->start_html(-title => 'Results', -bgcolor => $cgi->param('color')); if ($cgi->param()) { print $cgi->center( $cgi->h3("Hello: ". $cgi->param('name')), $cgi->p, "The keywords are: ",join(", ",$cgi->param('words')), $cgi->p, "Your favorite color is: ",$cgi->param('color')); } printing(); print $cgi->end_html(); sub printing { open(FILE, ">$file")|| die "Cannot save file $file "; print "test"; print FILE $cgi->param('name'); print FILE join(", ",$cgi->param('words')); print FILE $cgi->param('color'); close FILE; }

Comment on Cannot save to file with a CGI scipt
Download Code
Re: Cannot save to file with a CGI scipt
by kabel (Chaplain) on Sep 23, 2002 at 15:28 UTC
    print the variable $! in the die () call. the scalar contains the error string and will help you finding the error.
Re: Cannot save to file with a CGI scipt
by valdez (Monsignor) on Sep 23, 2002 at 15:38 UTC

    The funny thing happening here is that your web server runs under a different user, usually apache|nobody|www. Probably the directory you are trying to write into is owned by you and not by the user that runs httpd. So, if you check $! you will probably see a permission denied error.
    The solution is to change the owner of the directory where your script will write its files. Now you need to discover who should be the owner :)

    Ciao, Valerio

Re: Cannot save to file with a CGI scipt
by George_Sherston (Vicar) on Sep 23, 2002 at 21:42 UTC
    I think valdez is likely to be right - unless it's something even simpler, like the filepath you specified is wrong or incomplete. My only suggestion is that if you are looking in the error log for your error messages, then you could save yourself some trouble by putting
    use CGI::Carp qw(fatalsToBrowser warningsToBrowser); print $cgi->header; warningsToBrowser(1);
    near the top of your code during development - it'll chuck your fatal errors (and your warnings if you put  -w at the end of your shebang line) onto the browser where you can see 'em in real time.

    George Sherston
Re: Cannot save to file with a CGI scipt
by hacker_j99 (Beadle) on Oct 23, 2002 at 08:33 UTC
    I found this out the hard way
    the permissions on the data file need to be set at least to 766 to allow writing and reading

    here is a sample code that I was working on simular...
    #!/usr/bin/perl use CGI; $data = new CGI; $email = $data->param('email'); $datafile ="/full_path/data2.txt"; open (FORUM, "<$datafile"); @lines = <FORUM>; close (FORUM); $email="eww"; open (FORUM, ">$datafile"); print FORUM ("EMAIL"); print "Content-type: text/html\n\n"; print "$email";


    Hacker_J99
    UTC File Server for all your File needs

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://200130]
Approved by fireartist
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (11)
As of 2014-10-21 22:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (112 votes), past polls