【问题标题】:"Software error: File error, Permission denied". How to fix this error?“软件错误:文件错误,权限被拒绝”。如何修复此错误?
【发布时间】:2017-03-12 11:15:38
【问题描述】:

这个 perl 程序几乎显示了 cgi 脚本,但它在页面底部有这个注释

软件错误:文件错误,权限在 /Applications/XAMPP/xamppfiles/cgi-bin/asst5/orderbolts.cgi 第 34 行被拒绝。

cgi 的第 34 行是这样的:

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

这是html文件

<!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>

这是 CGI 文件

#!/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;

【问题讨论】:

    标签: html perl cgi


    【解决方案1】:

    更新:我的现在可以工作了。我做的事情是:

    1. 我将所有文件的共享和权限更改为读写
    2. chmod 765

    【讨论】:

      【解决方案2】:

      tl;博士

      提供bolts.txt 的完整路径,并使该路径对 Apache 用户可写(例如 www-data)。


      这里需要考虑两件事:

      1. 您尝试打开文件 bolts.txt 进行附加,但没有提供完全限定的路径名​​,例如/path/to/bolts.txt。在这种情况下,当 Apache 执行您的 .cgi 脚本时,您的文件将被搜索相对于当前工作目录 ($CWD)。我不确定它是否可以预测或记录在 Apache 执行 cgi-bin 脚本时 $CWD 是什么。 可能是 .cgi 脚本所在的目录,也可能是 Apache 自己的 bin 目录(如 /usr/sbin/),甚至是其他目录。为了避免这种不确定性,请始终提供/full/path/to/bolts.txt 或相对于您的 .cgi 脚本的路径。核心模块FindBin 非常适合:

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

        $FindBin::Bin 保存脚本的路径,在您的情况下,bolts.txt 的绝对路径将是 /Applications/XAMPP/xamppfiles/data/bolts.txt

      2. 您的 .cgi 脚本由您的网络服务器 (apache) 执行,因此以用户 www-runwwwwww-data 或类似用户身份运行,具体取决于您的操作系统和/或发行版。在我的情况下(Ubuntu)apache 以用户www-data 运行。一旦确定了bolts.txt 的实际位置,您必须确保用户www-data 有权写入该目录并写入该文件。对于我上面的示例,必要的命令可能是:

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

        ¹ 使 www-data 成为该目录的所有者
        ² 使其可写入www-data
        ³ 使其中的所有文件也可写入www-data

      【讨论】:

        【解决方案3】:

        使用 filename.ext 查找您的文件。授予它对可能想要在终端中访问它的软件的访问权限:

        sudo chmod 777 filename.ext
        

        值得仔细阅读 chmod 在这里所做的事情。但这可能会解决您的问题。

        【讨论】:

          猜你喜欢
          • 2015-02-02
          • 2018-07-28
          • 2021-11-24
          • 2021-07-22
          • 1970-01-01
          • 2018-03-22
          • 1970-01-01
          • 2016-09-25
          • 2015-09-16
          相关资源
          最近更新 更多