Solved

cgi form fail to write to the *.txt file?

Posted on 2000-02-16
7
242 Views
Last Modified: 2010-03-05
cgi form fail to write to the *.txt file?


I use the following cgi script file to write the date to the *.txt file, but I fail, how come?


the cgi is as following:

#!/bnr/projects/pegasus/perl5

#Change this path!

$COMMENT_FILE = "/hct_university/she/registrationDir/rs1.txt";

# The associative array $values will contain the data.

print "Content-type: text/html\n\n";
print "<html>\n";
print "<head>\n";

# Make sure it's a POST-method form submission
if (! &VerifyForm()) {
      print "<title>Not a POST Form Submission</title>\n";      
      print "</head>\n";
      print "<h1>Not a POST Form Submission</h1>\n";      
      print "This page should be accessed only by submitting\n";
      print "a form using the POST method. Perhaps your\n";
      print "browser does not support forms.\n";
      print "</body></html>\n";
      exit 0;
}

#OK, it's a form submission, so parse it.
&ParseForm();

#Use the information

#OK, we have all the data. Write it to a file in which
#we collect comments from users. Open to append, of course.

$fname = ">>" . $COMMENT_FILE;
open(OUT, $fname);

#The format I write here just happens to be appropriate for
#reading with a typical Unix mail reader; for instance,
#mail -f comments.txt should open the file.
#No subject lines, though.
print OUT "sir this is the testing";
#print OUT "Name: ", $FORM{'Name'},"\n";
#print OUT "Name: ", $values{'name'},"\n";
#print OUT "job title: ", $values{"job_title"},"\n";
#print OUT "Employee #: ", $values{"employee"},"\n";
#print OUT "LOB: ", $values{"lob"},"\n";
#print OUT "Dept #: ", $values{"dept_loc"},"\n";
#print OUT "Project Number : ", $values{"project_num"},"\n";
#print OUT " ESN Number: ", $values{"phone"},"\n";
#print OUT "Course Title: ", $values{"course_title"},"\n";
#print OUT "Course Number #: ", $values{"course_num"},"\n";
close(OUT);

print "<title>Thank you, ", $values{"name"}, "</title>\n";      
print "</head>\n";
print "<h1>Thank you, ", $values{"name"}, "</h1>\n";      
print "Thank you for your comments.\n";
print "</body></html>\n";
exit 0;

sub VerifyForm
{
      local($bad, $contentType, $requestMethod, $result);
      $bad = 0;
      # Check the content type of the data we've received
      $contentType = $ENV{"CONTENT_TYPE"};
      if ($contentType ne "application/x-www-form-urlencoded") {
            $bad = 1;
      }
      # And make sure the POST method was used
      $requestMethod = $ENV{"REQUEST_METHOD"};
      if ($requestMethod ne "POST") {
            $bad = 1;
      }

      $result = ! $bad;
}

sub ParseForm
{
      local($fields, $name, $value, $data);
      #Split standard input into fields

      read(STDIN, $data, $ENV{"CONTENT_LENGTH"});
      @fields = split(/&/, $data);

      #Split the fields into names and values, creating
      #an associative array indexed by the names

      foreach $item (@fields) {
            ($name, $value) = split(/=/, $item);
            $name = &UnescapeString($name);
            $value = &UnescapeString($value);
            $values{$name} = $value;
      }
}

#Unescape any special characters in a string

sub UnescapeString
{
        local($s) = $_[0];
      local($pos, $ascii);
      # Replace the + sign with spaces
      $s =~ s/\+/ /g;
      # Seek out and replace %xx hexadecimal escapes
      $pos = 0;
      while (($pos = index($s, "%", $pos)) != -1) {      
            $ascii = hex(substr($s, $pos + 1, 2));
            substr($s, $pos, 3) = pack("c", $ascii);
      }
      $s;
}
0
Comment
Question by:syu1
  • 4
  • 3
7 Comments
 
LVL 3

Expert Comment

by:guadalupe
ID: 2528836
Try adding these lines:

open(OUT, $fname);
print "Open message: $!\n";




Tis will give you the system message after opening the file and will let you know if there was an error.  It is a good idea to do somethink like this

open(OUT, $fname) || die "Failed to open $fname";
0
 

Author Comment

by:syu1
ID: 2528974
it seems the file can not be opened, it is there, and the path seems ok, I just dont know what is wrong the *txt file?

thanks!
0
 
LVL 3

Expert Comment

by:guadalupe
ID: 2529050
Did you try the solution I sent:

open(OUT, $fname);
print "Open message: $!\n";


What message did it print?

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:syu1
ID: 2529755
I try it, I got error message like : "no such file or directory found",

 thanks lot!
0
 
LVL 3

Expert Comment

by:guadalupe
ID: 2531068
OK so we know the problem is that some where the directory doesn't exist.  Notice I didn't say the file as perl create non existing files if you call then for write or append access.  But in this case it could create it because it was referenced to a dir which did't exist.
0
 

Author Comment

by:syu1
ID: 2532119
quadalupe:

would you pls explan it again, as   I dont quite follow your last explanation, thanks!
0
 
LVL 3

Accepted Solution

by:
guadalupe earned 50 total points
ID: 2534929
Ok the error message was that:

"no such file or directory found"

You opened the file like this:

$COMMENT_FILE = "/hct_university/she/registrationDir/rs1.txt";

$fname = ">>" . $COMMENT_FILE;

open(OUT, $fname);

the ">>" which you append onto the front of the file name means open the file for appending.  When perl is instructed to open a file for either writing or appending and it does not find the file it will create the FILE!!  Not any directories which may be part of thepath of the file but just the file.  But if the path doesn't exists it will be unable to create ther file and therefore unable to open it...  Therefore if it says the file or diectory doesn't exist it is much more lickly the problem is the dir.  Also if it was a lck of permissions to creat the file in the dir iot would say just that:

Permission denied

Check the spelling and location of the dir...
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now