1

Is there a way to make it where an Admin is able to edit php and html files?

I know theres this way, but it it will create files.

   <?php
    $myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
    $txt = "John Doe\n";
    fwrite($myfile, $txt);
    $txt = "Jane Doe\n";
    fwrite($myfile, $txt);
    fclose($myfile);
    ?>

I'm trying to make it where the admin is able to edit files from the admin panel, like Wordpress where in the admin panel your able to view and edit files. Is there a way I can do this? I already have an admin panel but I'm not able to figure it out. Any Ideas?

bigfella
  • 23
  • 7
  • You can read & write files using php, you can also display content on a page and submit content from a form. So - yes you can 8) – Andrew May 28 '18 at 19:20
  • https://stackoverflow.com/questions/17029917/how-to-write-php-code-to-a-file-with-php good luck. – Andrew May 28 '18 at 19:21
  • Wordpress is using DB records, not files. Most places that does this are using DBs, not files. – user3783243 May 28 '18 at 19:29
  • @user3783243 Wordpress allows you to edit theme files (PHP, CSS etc.) within the admin. I think that's what OP is after. – tobiv May 28 '18 at 20:51

1 Answers1

-1

You can do. I show you, how can you do this. Look at these two php files. This is the simplest method.

edit_file.php

<?php

$file = "1.html"; // file to edit
$html = file_get_contents($file); //read the file contents
$html = htmlentities($html, ENT_QUOTES);

?>

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Edit File</title>
<script>
function encode_content(){
  var content = document.getElementById('file_content').value;
  document.getElementById('file_content').value = encodeURIComponent(content);
}
</script>
</head>
<body>

<form name="edit_form" method="post" action="save_file.php" onsubmit="encode_content()">
  <textarea name="file_content" id="file_content" style="width:100%;" rows="20"><?php echo $html; ?></textarea>
  <input type="submit" value="Save this file">
</form>

</body>
</html>

save_file.php

<?php

$file_content = urldecode($_POST['file_content']);
file_put_contents('1.html', html_entity_decode($file_content)); //save the file

?>
Meskan
  • 79
  • 4