我想将文件保存为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);
?>
这听起来可能令人困惑,但有人可以给我小费吗?谢谢。
file_put_contents('variables.json', json_encode([
'ssh_user' => $_REQUEST['username'] ?? null,
'ssh_pw' => $_REQUEST['password'] ?? null,
]));
请注意,您的代码extract($_REQUEST)
可能会产生安全隐患。