Don
Don

Reputation: 235

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

I have a script to edit OpenVPN accounts (xxx.ovpn) from a dropdown list on my webserver, the script looks like this:

<?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);
 }
?>

The only part that isn't working is the save button, I tried with PHP (fopen, fwrite, perhaps I wrote the codes incorrectly) didn't work, and then I tried with bash also didn't work. Sorry I'm just a beginner in this so the possibility that I wrote incorrect codes is huge. The page looks like this: enter image description here

I want to save the file with the original name, the files are all located at /root/crt how should I fix this?

UPDATED: I closed the form </form> but still didn't work.

Upvotes: 0

Views: 153

Answers (1)

Oleg Dubas
Oleg Dubas

Reputation: 2348

<?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>';
?>

Upvotes: 1

Related Questions