【发布时间】: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