php - Raspberry pi 更新 json 文件时出现问题

标签 php linux apache raspberry-pi

我创建了一个网站,该网站根据用户输入更新 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/ 不幸的是,这不起作用。
有什么建议吗?

最佳答案

很可能这仍然是一个权限错误。

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

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

sudo chown -R www-data json

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

关于php - Raspberry pi 更新 json 文件时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49641405/

相关文章:

php - 如何计算特定组的多个时间戳值的时间差?

linux - 为什么 bash 插入额外的引号

linux - 什么 linux shell 命令返回字符串的一部分?

apache - 当我访问 HTTPS 时,网站一直重定向到 HTTP,原因不明

python - 域不提供 wsgi 文件,但 IP 提供

regex - htaccess : alternative to using <if>?

php - 如何调试Ajax响应中的500错误

php - 如何使用 PHP 从 Google 表格中删除行?

php - 'phpspec' 不是内部或外部命令,也不是可运行的程序或批处理文件

linux - 如何用sed注释掉第一次出现