CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Can't Open File From CGI Script

 



Sassy
Deleted

Mar 20, 2000, 8:04 PM

Post #1 of 6 (1220 views)
Can't Open File From CGI Script Can't Post

I have a very short cgi script in which I must open a file, output some text from a form to it and close it. The problem is I can't tell if the file is being created and if it is, I can't find it. Where would it go? If I run the script (minus the grabbing the form fields part) in Perl, the file gets created, I can add some static text to it and everything is fine. Why isn't this working for me?


Cretep
User

Mar 20, 2000, 9:55 PM

Post #2 of 6 (1220 views)
Re: Can't Open File From CGI Script [In reply to] Can't Post

Hi
Can you post your script as not it is impossible to troubleshoot with out it :-)

Regards

Peter Crouch


Cure
User

Mar 20, 2000, 10:35 PM

Post #3 of 6 (1220 views)
Re: Can't Open File From CGI Script [In reply to] Can't Post

Hi

You should use CGI.pm to parse your info.

this code will work for you::

#!/usr/local/bin/perl

use CGI;


open(OUTFILE, ">outfile.txt") or die "couldnt open outfile.txt $!";
print OUTFILE qq~

@{[$q->param('name')]},@{[$q->param('title')]},<br>
@{[$q->param('h_phone')]},@{[$q->param('email')]},<br>
@{[$q->param('sunsign')]},@{[$q->param('comments')]}
~;
close (OUTFILE);

}



Cure


darian
Deleted

Mar 20, 2000, 11:18 PM

Post #4 of 6 (1220 views)
Re: Can't Open File From CGI Script [In reply to] Can't Post

Try this:
use CGI qw(:cgi);
use CGI::Carp qw(fatalsToBrowser);
open(FILE, ">myfile.txt") or die("$!");
print FILE "My text.";
close(FILE) or die("$!");

using the or die("$!") along with CGI:Carp qw(fatalsToBrowser) will check to make sure the file was created or at least opened if it already existed. If not it will give you an error in your browser that the file could not be opened. Using it on the close portion simply checks to make sure ther were no errors when closing the file.


Sassy
Deleted

Mar 21, 2000, 6:32 AM

Post #5 of 6 (1220 views)
Re: Can't Open File From CGI Script [In reply to] Can't Post

Believe it or not, in 15 years of software engineering, I've never interacted with a user group so I don't know if you can attach files. It's a short script so here it is:

#!/usr/local/bin/perl

# This is a test script for InfiNET which parses the form and creates an output file.

if ($ENV{'REQUEST_METHOD'} eq 'POST') {

# Get the input

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

# Split the name-value pairs

@pairs = split(/&/, $buffer);

# Load the FORM variables

foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$FORM{$name} = $value;
}



# Open up the output file and make it comma delimited

open(OUTFILE, '>outfile.txt');

print OUTFILE "\"$FORM{name}\",";
print OUTFILE "\"$FORM{title}\",";
print OUTFILE "\"$FORM{h_phone}\",";
print OUTFILE "\"$FORM{w_phone}\",";
print OUTFILE "\"$FORM{email}\",";
print OUTFILE "\"$FORM{sunsign}\",";
print OUTFILE "\"$FORM{comments}\" ";

close (OUTFILE);

}

There was actually a little extra code that checked fields, but I removed it for simplicity sake. I really appreciate the help! - Sassy


Sassy
Deleted

Mar 21, 2000, 7:51 AM

Post #6 of 6 (1220 views)
Re: Can't Open File From CGI Script [In reply to] Can't Post

Also, Darian, when I use the "or die()" function, I get an error from the server. It's one of the generic, cryptic errors that tell you to contact the webmaster. I've tried different permutations of the file permissions and that doesn't help. In general, it feels like I'm working with a black box. I don't know how to use the debugging tools, I don't have access to the server log files and I'm pretty new to Perl (although it's very similar to C, thank God!) Your advice is appreciated.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives