【问题标题】:PHP fopen for writing fails用于编写的 PHP fopen 失败
【发布时间】:2015-12-02 02:00:16
【问题描述】:

我在处理 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!"); 行期间发生故障,出现“无法打开文件进行写入!”行。

我做错了什么?

编辑
我创建了一个测试 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 并且没有收到 die 警告。但是,当我运行代码时,我发现它没有写入文件,而是将其清除(清空了字符)。我的编写代码在上面,但为了清楚起见,我将其复制在下面:

$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>";

有什么建议吗?

【问题讨论】:

  • 如果删除or die部分会出现什么错误。
  • 有趣的是没有错误信息。该脚本只是继续并执行下一行,但它从不打开文件,因此它不会写入文件。它最终执行到最后一行。
  • 你能把file_get_contents 也取出来吗?也许它出于某种原因锁定了您的文件。
  • 你能ls -al stock.txt只是为了好玩吗?
  • 其实fileperms会更好:php.net/manual/en/function.fileperms.php

标签: php html fopen


【解决方案1】:

确保文件的所有者是 apache

命令是chown apache:apache stock.txt

它对我有用。

【讨论】:

    【解决方案2】:

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

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

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

    我希望这个答案可以帮助其他有类似问题的人。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多