PHP fopen 写入失败

PHP fopen for writing fails

我在使用我的 PHP 文件时遇到了一些问题。我的 fopen() 呼叫失败了,我不知道为什么。我对该文件拥有完全权限(当前设置为 777),我唯一能想到的是该文件已经打开,但我无法弄清楚为什么会这样。我的代码由登陆 PHP 页面和操作 PHP 页面组成。着陆页如下:

<!DOCTYPE HTML>
<html>
<head>
    <title>Canteen Manager</title>
    <link type="text/css" rel="stylesheet" href="style_common.css"/>
    <link type="text/css" rel="stylesheet" href="style_addstock.css"/>
</head>
<body>
<div class="header">
    <img src="images/aafclogo.png" class="aafclogo">
    <h3>Add Stock</h3>
</div>
<p>Select Product:</p>
<form action="addstockaction.php" method="post" name="formExistingProduct">
    <input type='hidden' name='productType' value='existing'> 
    <select name="products">
        <?php
            class Product {
                public $name = "";
                public $amount = "";
            }

            $myfile = fopen("stock.txt", "r") or die("Unable to open file!");
            while(!feof($myfile)) {
              $fileContents = $fileContents . fgets($myfile);
            }
            fclose($myfile);
            $dictionary = json_decode($fileContents, true);

            for ($i = 0; $i < count($dictionary); ++$i) {
                echo "<option value=" . $i . ">" . $dictionary[$i]['name'] . "    </option>";
            }
        ?>
    </select>
    <input type="number" name="txtNumber" min="1"><br><br>
    <input type="submit" value="Update Amount">
</form>

当用户提交表单时,它会调用以下 PHP 文件:

<!DOCTYPE HTML>
<html>
<head>
    <title>Canteen Manager</title>
    <link type="text/css" rel="stylesheet" href="style_common.css"/>
</head> 
<body>
<?php
        /*$myfile = fopen("stock.txt", "r") or die("Unable to open file!");
        while(!feof($myfile)) {
          $fileContents = $fileContents . fgets($myfile);
        }
        fclose($myfile);*/
        $fileContents = file_get_contents("stock.txt");
        echo $fileContents;
        $dictionary = json_decode($fileContents, true);

        $dictionary[$_POST['products']]['amount'] = $dictionary[$_POST['products']]['amount'] + $_POST['txtNumber'];
        $json = json_encode($dictionary, true);

        $writeFile = fopen("stock.txt", "w") or die("Unable to open file for writing!");
        $txt = $json;
        fwrite($writeFile, $txt);
        fclose($writeFile);
        echo "<p><strong>Added " . $_POST['txtNumber'] . " items to " . $dictionary[$_POST['products']]['name'] . "</strong></p><br><br>";
?>

<br><br><div class="buttonContainer">
    <a href="addstock.php"><div class="button">
        <p>Add More Items</p>
    </div></a>
    <a href="index.php"><div class="button">
        <p>Home</p>
    </div></a>
</div>

操作 PHP 文件在 $writeFile = fopen("stock.txt", "w") or die("Unable to open file for writing!"); 行期间失败,当它出现在 'Unable to open file for writing!' 行时。

我做错了什么?

编辑
我创建了一个测试 PHP 文件(名为 test.php)并放置在服务器上与我当前文件相同的位置。该文件仅包含以下代码:

<!DOCTYPE HTML>
<html>
<head>
    <title>Canteen Manager</title>
    <link type="text/css" rel="stylesheet" href="style_common.css"/>
    <link type="text/css" rel="stylesheet" href="style_addstock.css"/>
</head>
<body>
<?php
        $writeFile = fopen("stock.txt", "w") or die("Unable to open file for writing!");
        $txt = $json;
        fwrite($writeFile, '[{"name":"Boost","amount":10},{"name":"Chicken Noodles","amount":10},{"name":"Twix","amount":10}]');
        //fwrite($writeFile, "test");
        fclose($writeFile);
        echo "<strong>Success</strong>";
?>
</body>
</html>

当我 运行 最初,它成功打开文件,但不会写入。相反,它会清除它。自从将该代码复制到我的实时文件后,这两个文件都出现了我之前遇到的错误。对我来说,这支持文件在某处打开因此无法再次打开的理论。我认为它可能是我的 FTP 客户端,所以我关闭它并尝试了,但同样的错误。我已确认该文件 (stock.txt) 未在我的浏览器中打开,也未被我笔记本电脑上的任何程序使用。

我真的卡在这上面了。有没有人有更好的方法来写入文件(请记住,此时我正在覆盖文件)。我真的很感激任何建议,无论是关于解决我的问题,还是解决问题的不同方法。

编辑 2
尽管我将文件权限设置为 777,但我发现文件权限设置为 644,因此我将它们设置回 777,但没有收到死亡警告。但是,当我 运行 代码时,我发现它并没有写入文件,而是只是清除了它(清空了它的字符)。我写的代码在上面,但为了清楚起见,我将其复制在下面:

$fileContents = file_get_contents("stock.txt");
echo $fileContents;
$dictionary = json_decode($fileContents, true);

$dictionary[$_POST['products']]['amount'] = $dictionary[$_POST['products']]['amount'] + $_POST['txtNumber'];
$json = json_encode($dictionary, true);

    $writeFile = fopen("stock.txt", "w") or die("Unable to open file for writing!");
$txt = $json;
fwrite($writeFile, $txt);
fclose($writeFile);
echo "<p><strong>Added " . $_POST['txtNumber'] . " items to " . $dictionary[$_POST['products']]['name'] . "</strong></p><br><br>";

有什么建议吗?

确保文件的所有者是 apache

命令是 chown apache:apache stock.txt

对我有用。

我最终解决了这个问题,这在很大程度上要感谢@ParrisVarney 的帮助。问题出在文件权限中。每次我上传文件的新版本(PHP 或 TXT),或在我的 FTP 客户端中编辑 stock.txt 文件的在线版本时,它都会重置文件权限到 644。我必须手动将它们改回 777,然后才能在每次上传后继续,然后才能打开文件。

关于写入问题,文本文件被清除的地方,是因为我的json_encode调用不正确。我有一个布尔值作为导致它失败的属性。通过从 $json = json_encode($dictionary, true); 中删除 true 并使其成为 $json = json_encode($dictionary);,我能够让脚本写入数据。

感谢大家的帮助,不胜感激

我希望这个回答可以帮助其他遇到类似问题的人。