【发布时间】:2018-04-04 01:33:46
【问题描述】:
我创建了一个根据用户输入更新 json 文件的网站。
我在我的 windows 电脑上开发了这个,测试了运行 WAMP 的站点,它工作正常。
我想把它放在我的树莓派上,它正在运行 apache2 和 php5。我已将该网站放在 /var/www/html/ 中,该网站可以正确加载并正常运行。除了更改页面上的设置外,json 文件永远不会更新。
这是php文件:
<?php
$LightOne = $_POST['lightOne'];
$LightTwo = $_POST['lightTwo'];
$LightThree = $_POST['lightThree'];
$ActivateLightsJson = file_get_contents('json/ActivateLights.json');
$ActivateLights = json_decode($ActivateLightsJson, true);
$ActivateLights["lightOne"] = $LightOne;
$ActivateLights["lightTwo"] = $LightTwo;
$ActivateLights["lightThree"] = $LightThree;
$fp = fopen('json/ActivateLights.json', 'w');
fwrite($fp, json_encode($ActivateLights));
fclose($fp);
?>
调用php的js:
function saveJson(){
lightOne = document.getElementById("lightOne").checked ? 1 : 0;
lightTwo = document.getElementById("lightTwo").checked ? 1 : 0;
lightThree = document.getElementById("lightThree").checked ? 1 : 0;
$.post('saveJson.php',{lightOne:lightOne, lightTwo:lightTwo, lightThree:lightThree}, function(data){});
}
我使用 /www 更新了权限
sudo chown $logname /var/www/ 不幸的是,这不起作用。
有什么建议吗?
【问题讨论】:
-
您在
ls -l中看到的目录和文件 的权限是什么?该代码没有任何问题,因此问题必须出在其他地方。 -
@VoteyDisciple - 如果我在这里狙击了你的潜在答案,我很抱歉。直到我提交了可能的答案后,我才注意到您的评论。从您的问题中获得适当的数据后,如果您想发布,我可以删除。
-
对json文件的权限总共是4 -rw-r---r-- 1 pi pi 48 data.json
-
另外,在上面的评论中,您提到该文件名为“data.json”,但在您的代码中它被命名为“ActivateLights.json”。 /var/www/html/json 目录中的文件名需要是 ActiveLights.json(区分大小写)以匹配您的代码,并且必须使用以下命令归“www-data”所有。
标签: php linux apache raspberry-pi