PHP 不会保存我在 textarea 中编辑过的文件
PHP won't save my file that's been edited in textarea
我有一个脚本可以从我的网络服务器上的下拉列表中编辑 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,也许我写错了代码)没有用,然后我用[=试了25=] 也没有用。抱歉,我只是这方面的初学者,所以我写错代码的可能性很大。该页面如下所示:
我想用原来的名字保存文件,文件都在/root/crt
我该如何解决?
更新:我关闭了表单 </form>
但仍然没有用。
<?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>';
?>
我有一个脚本可以从我的网络服务器上的下拉列表中编辑 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,也许我写错了代码)没有用,然后我用[=试了25=] 也没有用。抱歉,我只是这方面的初学者,所以我写错代码的可能性很大。该页面如下所示:
我想用原来的名字保存文件,文件都在/root/crt
我该如何解决?
更新:我关闭了表单 </form>
但仍然没有用。
<?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>';
?>