I'm currently trying to use file_put_contents
to update a JSON file of mine. This is what my PHP currently looks like:
我目前正在尝试使用file_put_contents来更新我的JSON文件。这就是我的PHP现在的样子:
$result = file_put_contents('Resources/logs/file.json', $newJsonString);
if($result === true) {
echo "true";
} else {
echo "false";
}
However, it always returns false
. Nevertheless, I have set the directory permissions to 777 for BOTH Resources
and logs
. I have also set file permissions to 777 for file.json
.
但是,它总是返回false。不过,我将资源和日志的目录权限设置为777。我还将file.json的文件权限设置为777。
The path is also correct, as file_get_contents
works with the same path from the same file.
路径也是正确的,因为file_get_contents使用来自同一文件的相同路径。
To further prove my point, I have also ran sudo chown -R www-data:www-data /var/www/html/core/Resources/
to give permission to the directory Resources
(and everything within) to be written to by the user www-data
(which I'm assuming would allow the JSON file to be written to by running the PHP script online).
为了进一步证明我的观点,我还运行了sudo chown -R www-data:www-data /var/www/html/core/Resources/,允许用户www-data写入目录资源(以及其中的所有内容)(我假设通过在线运行PHP脚本可以写入JSON文件)。
I don't know if this would be helpful, but I'm also running this PHP script by sending POST
data to it via JavaScript from a different server/website.
我不知道这是否有用,但我也在运行这个PHP脚本,通过从另一个服务器/网站向它发送POST数据。
Why in the world is file_put_contents
STILL returning false and not writing to the file? It always makes the JSON file blank after it is ran.
为什么file_put_contents仍然返回false而不写入文件?它总是使JSON文件在运行后为空。
Supplementary Code:
补充代码:
This is the whole chunk of code, including the file_put_contents
section:
这是整个代码块,包括file_put_contents部分:
$jsonString = file_get_contents('Resources/logs/file.json');
$data = json_decode($jsonString);
$data->coins = $data->coins + $coins;
$data->uses = $data->uses + 1;
print_r($data);
$newJsonString = json_encode($data);
$result = file_put_contents('Resources/logs/file.json', $newJsonString);
if($result === true) {
echo "true";
} else {
echo "false";
}
...and this is the JSON file:
…这是JSON文件
{"coins":"0","uses":"0"}
Any help would be highly appreciated, I've been trying to fix this issue for one whole day already.
非常感谢您的帮助,我已经花了一整天的时间来解决这个问题。
Edit 1:
编辑1:
I forgot to mention this, but this code was working perfectly fine yesterday for about an hour. Then all of a sudden it stopped working and started making the JSON file blank.
我忘了说了,但是这段代码在昨天已经运行了一个小时了。然后,它突然停止工作,开始使JSON文件为空。
Edit 2:
编辑2:
I have just cleared the Apache error log, and the script it working again (which is good). However, file_put_contents
is still returning false
. What is going on...
我刚刚清除了Apache错误日志,它的脚本再次工作(这很好)。但是,file_put_contents仍然返回false。是怎么回事……
Edit 3:
编辑3:
Thanks to Laurent Wartel I have updated my if statement for file_put_contents
and it seems like it is no longer returning false.
感谢Laurent Wartel,我更新了file_put_contents的if语句,它似乎不再返回false。
if($result === false) {
echo "Error";
} else {
echo "All good, $result bytes written";
}
3 个解决方案
#1
1
Try to write a string to your file. This way you can determine if your problem resides on the file writing or the content generation:
尝试写一个字符串到你的文件。通过这种方式,您可以确定您的问题是存在于文件编写还是内容生成:
file_put_contents('Resources/logs/file.json', 'Hello, file');
Also, I'd add a LOCK_EX flag to the file write call to make sure you are not running into concurrency issues.
另外,我将向文件写入调用添加一个LOCK_EX标志,以确保不会出现并发问题。
#2
0
From the doc PHP: file_put_contents - Manual
来自doc PHP: file_put_contents -手册
This function returns the number of bytes that were written to the file, or FALSE on failure. Warning
该函数返回被写入文件的字节数,或在失败时返回FALSE。警告
This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE. Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.
此函数可以返回布尔值FALSE,但也可以返回一个非布尔值,该值的计算值为FALSE。请阅读关于布尔人的部分以获得更多信息。使用===操作符测试这个函数的返回值。
Modify your code...
修改代码…
if($result === false) {
echo "Error";
} else {
echo "All good, $result bytes written";
}
#3
0
http://php.net/manual/en/function.file-put-contents.php
http://php.net/manual/en/function.file-put-contents.php
This function returns the number of bytes that were written to the file, or FALSE on failure.
该函数返回被写入文件的字节数,或在失败时返回FALSE。
You can't user the identical comparison operator to check for success. Both of this alternatives are valid:
您不能使用相同的比较运算符来检查是否成功。这两种选择都是有效的:
if($result) {
echo "true";
} else {
echo "false";
}
or
或
if($result === false) {
echo "false";
} else {
echo "true";
}
#1
1
Try to write a string to your file. This way you can determine if your problem resides on the file writing or the content generation:
尝试写一个字符串到你的文件。通过这种方式,您可以确定您的问题是存在于文件编写还是内容生成:
file_put_contents('Resources/logs/file.json', 'Hello, file');
Also, I'd add a LOCK_EX flag to the file write call to make sure you are not running into concurrency issues.
另外,我将向文件写入调用添加一个LOCK_EX标志,以确保不会出现并发问题。
#2
0
From the doc PHP: file_put_contents - Manual
来自doc PHP: file_put_contents -手册
This function returns the number of bytes that were written to the file, or FALSE on failure. Warning
该函数返回被写入文件的字节数,或在失败时返回FALSE。警告
This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE. Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.
此函数可以返回布尔值FALSE,但也可以返回一个非布尔值,该值的计算值为FALSE。请阅读关于布尔人的部分以获得更多信息。使用===操作符测试这个函数的返回值。
Modify your code...
修改代码…
if($result === false) {
echo "Error";
} else {
echo "All good, $result bytes written";
}
#3
0
http://php.net/manual/en/function.file-put-contents.php
http://php.net/manual/en/function.file-put-contents.php
This function returns the number of bytes that were written to the file, or FALSE on failure.
该函数返回被写入文件的字节数,或在失败时返回FALSE。
You can't user the identical comparison operator to check for success. Both of this alternatives are valid:
您不能使用相同的比较运算符来检查是否成功。这两种选择都是有效的:
if($result) {
echo "true";
} else {
echo "false";
}
or
或
if($result === false) {
echo "false";
} else {
echo "true";
}