我有一个脚本可以从我的网络服务器的下拉列表中编辑OpenVPN帐户(xxx.ovpn),脚本如下所示:
<?php
echo "<form action=\"".$PHP_SELF."\" method=\"post\">";
echo "</br>";
exec('ls /root/crt |grep -i -e .ovpn -e .txt',$list);
echo 'Config: <select name="config">';
$x=0;
while($x<count($list)){
if($list[$x]==$h[0]){
echo "<option value=\"/root/crt/$list[$x]\" selected>$list[$x]</option>";}
else{
echo "<option value=\"/root/crt/$list[$x]\">$list[$x]</option>";}
$x++;
}
echo "<input name=\"edit\" type=\"submit\" value=\"Edit\"/><br>";
if(isset($_POST["edit"])) {
$filename = $_POST["config"];
$myfile = fopen("$filename", "r") or die("Unable to open file!");
$filecontent = fread($myfile,filesize("$filename"));
fclose($myfile);
$nameonly = str_replace('/root/crt/', '', $filename);
echo 'Showing: '.$nameonly.'<br><textarea name="akunvpn" autofocus rows="16" cols="78" style="font-family: Arial;font-size: 9pt;">';
if(isset($filecontent)) { echo $filecontent; }
echo '</textarea><br><br>
<input name="save" type="submit" value="Save">
'; //echos file content in textarea.
}
//write to text file
if(isset($_POST["save"])) {
$save = $_POST["akunvpn"];
exec('echo '.$save.' >> '.$filename);
}
?>
唯一没有工作的部分是保存按钮,我尝试使用PHP(fopen,fwrite,也许我写的代码不正确)没有工作,然后我尝试使用bash也没有#&# 39;工作。对不起,我只是一个初学者,所以我编写错误代码的可能性很大。该页面如下所示:
我想用原始名称保存文件,文件都位于/root/crt
我该如何解决?
更新:我关闭了</form>
表单,但仍然无效。
答案 0 :(得分:1)
<?php
echo "<form action=\"\" method=\"post\">";
echo "</br>";
exec('ls /root/crt |grep -i -e .ovpn -e .txt',$list);
echo 'Config: <select name="config">';
$x=0;
while($x<count($list)){
if($list[$x]==$h[0]){
echo "<option value=\"/root/crt/$list[$x]\" selected>$list[$x]</option>";}
else{
echo "<option value=\"/root/crt/$list[$x]\">$list[$x]</option>";}
$x++;
}
echo '</select>';
echo "<input name=\"edit\" type=\"submit\" value=\"Edit\"/><br>";
if(isset($_POST["edit"]))
{
$filename = $_POST["config"];
$filecontent = file_get_contents($filename);
$nameonly = str_replace('/root/crt/', '', $filename);
echo 'Showing: '.$nameonly.'<br><textarea name="akunvpn" autofocus rows="16" cols="78" style="font-family: Arial;font-size: 9pt;">';
echo htmlentities($filecontent);
echo '</textarea><br><br>
<input type="hidden" name="filename" value="'.$filename.'">
<input name="save" type="submit" value="Save">
'; //echos file content in textarea.
}
//write to text file
if(isset($_POST["save"])) {
file_put_contents($_POST['filename'], $_POST["akunvpn"]);
}
echo '</form>';
?>