我想將文件保存為 JSON,使用 PHP 的 JSON 格式。目前我有這段代碼,但variables.txt我想保存variables.json為 JSON 文件格式,而不是 ,如下所示: { ssh_user: teste, ssh_pw: teste}這是我的 PHP 代碼:<?php extract($_REQUEST); $file=fopen("variables.txt","w"); fwrite($file,"ssh_user: "); fwrite($file, $username ."\n"); fwrite($file,"ssh_pw: "); fwrite($file, $password ."\n"); fclose($file);?>這聽起來可能令人困惑,但有人可以給我提示嗎?謝謝。
1 回答

瀟瀟雨雨
TA貢獻1833條經驗 獲得超4個贊
file_put_contents('variables.json', json_encode([
'ssh_user' => $_REQUEST['username'] ?? null,
'ssh_pw' => $_REQUEST['password'] ?? null,
]));
請注意,您的代碼extract($_REQUEST)可能具有安全隱患。
- 1 回答
- 0 關注
- 232 瀏覽
添加回答
舉報
0/150
提交
取消