這是我當前的JSON文件:PHP JSON file_put_contents空白
{"coins":"0","uses":"0"}
...我重寫值用下面的PHP:
$data = json_decode(file_get_contents('test.json'), true);
$data["coins"] = iconv($data["coins"] + $coins);
$data["uses"] = iconv($data["uses"] + 1);
file_put_contents('test.json', json_encode($data));
我打印出來的$data
值並且已經看到它包含了新的價值。然而,file_put_contents
寫入的文件總是空白。有關這是爲什麼的任何建議?
編輯:
這是$data
回報:
Array ([coins] => 100 [uses] => 1)
改變了我的代碼一點點後,好像file_put_contents
回報false
。不過,我已將文件權限設置爲(test.json
)。
調用'file_put_contents()'調用的返回值是什麼? 'true'還是'false'?我假設「錯誤」,這在大多數情況下導致系統爲正在運行PHP的用戶授予的寫權限不足。 – TiMESPLiNTER
可能您缺少對目標文件/目標的寫入權限? – Steini
@TiMESPLiNTER:由於文件已打開,且以前的內容已被刪除,因此權限不足無法成爲問題的根源。 – mcserep