PHP function.fopen 无法打开流:权限被拒绝

PHP function.fopen failed to open stream: Permission denied

我 运行 apache ubuntu 12,我无法使用 fopen 函数打开特定文件。我可以在同一个 php 文件中打开其他文件。 这是错误:

Warning: fopen(file.txt) [function.fopen]: failed to open stream: Permission denied in /.../.../.../.../....php on line 10
Warning: fwrite() expects parameter 1 to be resource, boolean given in /.../.../.../.../....php on line 11
Warning: fclose() expects parameter 1 to be resource, boolean given in /.../.../.../.../....php on line 12
Warning: Cannot modify header information - headers already sent by (output started at /.../.../.../.../....php:10) in /.../.../.../.../....php on line 20

我的代码:

<?php
    session_start();
    $username = $_SESSION['username'];
    $amount = $_SESSION['amount'];
    $message = $_SESSION['message'];
    $send = "$username donated $amount";
    $Vdata = file_get_contents('donation.txt');
    $cumoney = file_get_contents('money.txt');
    $newmoney = $amount + $cumoney;
    $handle4 = fopen("money.txt", "w");
    fwrite($handle4, $newmoney);
    fclose($handle4);
    if($Vdata == $send){
        $handle = fopen("donation.txt", "w");
        fwrite($handle, $username ." donated  ". $amount);
        fclose($handle);
        $handle2 = fopen("donationmsg.txt", "w");
        fwrite($handle2, $message);
        fclose($handle2);
        header ("Location: ...");
    }else{
        $handle = fopen("donation.txt", "w");
        fwrite($handle, $username ." donated ". $amount);
        fclose($handle);
        $handle2 = fopen("donationmsg.txt", "w");
        fwrite($handle2, $message);
        fclose($handle2);
        header ("Location: ...");
    }
?>

这看起来像是权限问题。您应该确保您和您的脚本将有权访问该文件和包含它的目录。

显然 PHP 没有文件的写权限。 这在 Ubuntu PHP 上以与 Apache 相同的用户身份运行,因此请确保该文件可由 www-data 组写入。

user@host:/path/to/your/file# chgrp www-data yourfile.txt
user@host:/path/to/your/file# chmod g+w yourfile.txt