【问题标题】:Raspberry pi issues updating json fileRaspberry pi 更新 json 文件时出现问题
【发布时间】: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


【解决方案1】:

很可能仍然是权限错误。

“json”目录和文件本身必须由 apache 用户拥有或世界可写(通常认为不太理想)。您发出的命令不是递归的,因此它没有更改相应的文件。此外,$LOGNAME(区分大小写)是当前登录的用户,而不是 Apache 进程,它可能类似于“www-data”,具体取决于您的 linux 发行版。

所以,你可能需要这样的东西:

sudo chown -R www-data json

这将允许 apache 进程写入您的 JSON 文件及其父 (json) 目录,假设您从“/var/www/html”目录运行它。

【讨论】:

  • 我的站点在 /var/www/html/ 中,当我运行您提供的命令时,该文件夹位于 html 文件夹 /var/www/html/json 中的 json 文件夹中,它显示“chown cannot访问'json':没有这样的文件或目录'。对不起,我是 linux 命令的新手
  • 从 /var/www/html 目录试试 - 你只需要在包含“json”目录的文件夹中运行它。如果您需要进入该目录,您可以“cd /var/www/html”或者您可以将命令运行为“sudo chown -R www-data /var/www/html/json”,这将更改权限 no无论你从哪里运行它,因为它引用了整个路径。
  • 谢谢你,你给了我正确的方向。最终只是运行 chmod 777 Data.json 不是很安全,但它只是用于个人项目,所以没关系。再次感谢您。
猜你喜欢
  • 2014-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多