iT邦幫忙

0

修改本端的 json 檔?不透過開啟檔案編輯和IDE?

我有一個檔案是 xxx.json
裡面內容是一些本端基本的設定
假設這樣

{
  "DB_USERNAME": "root",
  "DB_PASSWORD": "12ko31ko2k3fef",
  "ROBOTS": "noindex, nofollow"
}

能用什麼方式可以不必透過打開這個檔案編輯,或是用IDE編輯
可以直接在網頁上用表單方式直接修改 xxx.json 或其他方式
這是有辦法做到的嗎?

Han iT邦研究生 1 級 ‧ 2019-08-16 09:51:06 檢舉
php 用 file_put_contents 去改內容囉
火爆浪子 iT邦研究生 1 級 ‧ 2019-08-16 10:11:02 檢舉
等於是把內容轉成 json 丟進去這檔案嗎
Han iT邦研究生 1 級 ‧ 2019-08-16 11:19:53 檢舉
是的
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

3
Han
iT邦研究生 1 級 ‧ 2019-08-16 11:16:57
最佳解答

Before

{
  "DB_USERNAME": "root",
  "DB_PASSWORD": "12ko31ko2k3fef",
  "ROBOTS": "noindex, nofollow"
}
// filePut.php
<?php
// 這邊就看你要怎接前端給的資料了,用Get或Post之類的

$newData = [
  "DB_USERNAME" => "root",
  "DB_PASSWORD" => "1234",
  "ROBOTS" => "noindex, nofollow"
] ;

file_put_contents('xxx.json', json_encode($newData)) ;

After

{
  "DB_USERNAME": "root",
  "DB_PASSWORD": "1234",
  "ROBOTS": "noindex, nofollow"
}

我要發表回答

立即登入回答