0
votes

This perl program pretty much shows the cgi script but it has this note at the bottom of the page

Software error: File error, Permission denied at /Applications/XAMPP/xamppfiles/cgi-bin/asst5/orderbolts.cgi line 34.

Line 34 from cgi is this:

open (ORDERS, ">>bolts.txt") or die "File error, $!";

Here is the html file

<!doctype html>
<html lang="en">
<body>

<form action="/cgi-bin/asst5/orderbolts.cgi" method="post">
<table border="2" cellspacing="5" cellpadding="5">
<tr>
   <td align="center">Name : </td>
   <td><input type="text" name="customer" size="15"></td>
</tr>
<tr>
   <td align="center">Street : </td>
   <td><input type="text" name="street" size="15"></td>
</tr>
<tr>
   <td align="center">City : </td>
   <td><input type="text" name="city" size="15"></td>
</tr>
<tr>
   <td align="center">State : </td>
   <td><input type="text" name="state" size="3"></td>
</tr>
<tr>
   <td align="center">Zip : </td>
   <td><input type="text" name="zip" size="6"></td>
</tr>
<tr>
   <td align="center"># of bolts : </td>
   <td><input type="text" name="qty" size="4"></td>
</tr>
<tr>
   <td colspan="2" align="center"><input type="submit" value="Check Out"></td>
</tr>
</table>
</form>
</div>
</body>
</html>

Here is the CGI file

#!/usr/bin/perl -w

use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser);
use strict;
use warnings;
print "Content-type: text/html\n\n";

my $cust = param('customer');
my $addr = param('street');
my $city = param('city');
my $state = param('state');
my $zip = param('zip');
my $qty = param('qty');
my $pay = sprintf("\$%.2f", $qty * 0.95);

print <<HERE;
<HTML>
<BODY>
<H3>Here is your order...please check</H3>
$cust<br>
$addr<br>
$city , $state $zip<br>
Number of bolts ordered : $qty<br>
Total bill for order : $pay<br><br>
<i><b>Thanks for shopping at Brad's</b></i>
HERE

open (ORDERS, ">>bolts.txt") or die "File error, $!";
print ORDERS "$cust|$addr|$city|$state|$zip|$qty|$pay\n";
close ORDERS;
3

3 Answers

0
votes

Find your file with filename.ext. Give it access permissions to software that may want to access it with this in a terminal:

sudo chmod 777 filename.ext

It is worthwhile to read up on exactly what chmod is doing here. But this will probably solve your problem.

0
votes

tl;dr

Give a full path to bolts.txt and make that path writeable for the Apache user (e.g. www-data).


Two things to consider here:

  1. You try to open the file bolts.txt for appending but don't give a fully qualified path name, like e.g. /path/to/bolts.txt. In that case your file will be searched for relative to the current working directory ($CWD) when Apache executes your .cgi script. I'm not sure whether its predictable or documented what the $CWD is when Apache executes cgi-bin scripts. It might be the directory where the .cgi script is located, it might also be Apache's own bin directory (like /usr/sbin/) or even something else. To get around this uncertainty always give a /full/path/to/bolts.txt or a path relative to your .cgi script. The core module FindBin is great for that:

    use FindBin;
    ...
    open (ORDERS, '>>', "$FindBin::Bin/../../data/bolts.txt")
    

    $FindBin::Bin holds the path of your script and in your case the absolute path to bolts.txt would then be /Applications/XAMPP/xamppfiles/data/bolts.txt.

  2. Your .cgi script is executed by your webserver (apache) and thus runs as user www-run or www or www-data or something similar, depending on your OS and/or distribution. In my case (Ubuntu) apache runs as user www-data. Once you figured out where your bolts.txt is actually located you must make sure the user www-data has permission to write in that directory and write to that file. For my example above the necessary commands could be:

    chown www-data /Applications/XAMPP/xamppfiles/data  # ¹
    chmod 755 /Applications/XAMPP/xamppfiles/data       # ²
    chmod -R u+w /Applications/XAMPP/xamppfiles/data/*  # ³
    

    ¹ make www-data the owner of that directory
    ² make it writeable for www-data
    ³ make all files in there also writeable for www-data

-2
votes

Update: Mine works now. The things I did are:

  1. I changed the sharing & permissions to read & write for all files
  2. chmod 765