繁体   English   中英

PHP不会保存在textarea中编辑过的文件

[英]PHP won't save my file that's been edited in textarea

我有一个脚本,可从Web服务器上的下拉列表中编辑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也不起作用。 抱歉,我只是一个初学者,所以我编写错误代码的可能性很大。 该页面如下所示: 在此处输入图片说明

我想用原始名称保存文件,这些文件都位于/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>';
?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM