用于写入的PHP fopen失败

用于写入的PHP fopen失败,php,html,fopen,Php,Html,Fopen,我在使用PHP文件时遇到了一些问题。我的fopen()调用失败,我无法找出原因。我对该文件拥有完全权限(当前设置为777),我唯一能想到的是该文件已经打开,但我不知道为什么会这样。我的代码由一个登录PHP页面和一个动作PHP页面组成。登陆页面如下: <!DOCTYPE HTML> <html> <head> <title>Canteen Manager</title> <link type="text/css" r

我在使用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>
当我开始运行这个程序时,它成功地打开了文件,但没有写入。相反,它会清除它。由于将代码复制到我的live文件中,这两个文件都会出现我之前遇到的错误。对我来说,这支持了文件在某处打开,因此无法再次打开的理论。我想它可能是我的FTP客户端,所以我关闭了它并尝试了,但同样的错误。我已经确认该文件(stock.txt)没有在我的浏览器中打开,也没有被我笔记本电脑上的任何程序使用

我真的被这件事缠住了。有没有人有更好的方法来写入文件(请记住,我现在写文件太多了)。我真的很感激任何建议,无论是关于解决我的问题,还是以不同的方式解决问题

编辑2
我发现文件权限设置为644,尽管我将它们设置为777,所以我将它们设置回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>";
$fileContents=文件获取内容(“stock.txt”);
echo$fileContents;
$dictionary=json_decode($fileContents,true);
$dictionary[$\u POST['products']['amount']=$dictionary[$\u POST['products']['amount']+$\u POST['txtNumber'];
$json=json_encode($dictionary,true);
$writeFile=fopen(“stock.txt”,“w”)或die(“无法打开文件进行写入!”);
$txt=$json;
fwrite($writeFile,$txt);
fclose($writeFile);
回声“已添加”$_POST['txtNumber']。“项目至”$字典[$\u POST['products']['name']。“



”;

有什么建议吗?

确保文件的所有者是apache

命令为chown apache:apache stock.txt


这对我很管用。

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

至于写入问题,文本文件被清除了,这是因为我的
json\u encode
调用不正确。我有一个布尔值作为一个属性,导致它失败。通过从
$json=json\u encode($dictionary,true)中删除
true
并使其仅为
$json=json\u encode($dictionary),我能够得到脚本来写数据

谢谢大家的帮助,非常感谢


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

如果您删除
或死亡
部分,会出现什么错误。有趣的是,没有错误消息。脚本只是继续并执行下一行,但它从未打开文件,因此不会写入。它最后执行到最后一行。你能同时取出
文件\u get\u contents
位吗?可能是出于某种原因锁定了你的文件。你能仅仅为了好玩而
ls-al stock.txt
吗?实际上,fileperm会更好:
<!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>
$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>";