如何使用Admin面板中的PHP编辑和保存PHP文件



我需要从管理面板编辑PHP文件。我怎样才能做到这一点?我已经在WordPress和Joomla等CMS中看到了这一点。有PHP库或其他东西吗?

我为此目的推荐ACE。这是用JavaScript构建的代码编辑器:https://ace.c9.io/

要在编辑后读取文件并保存文件,您可以使用诸如file_get_contents()file_put_contents()的PHP功能中的内置。

这是一个最小的工作示例:

<?php
if (isset($_POST['code'])) {
    file_put_contents('myfile.php', $_POST['code']);
}
$code = htmlentities(file_get_contents('myfile.php')); 
?>
<!DOCTYPE html>
<html>
<head>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.3.0/ace.js"></script>
</head>
<body>
<form action="" method="post">
    <textarea id="source" name="code" autocomplete="off" style="display: none"><?=$code?></textarea>
    <div id="editor" style="min-height: 250px"><?=$code?></div>
    <input type="submit" />
</form>
<script>
    var tarea = document.getElementById('source');
    var code = tarea.value;
    var editor = ace.edit("editor");
    editor.session.setValue(code);
    editor.setTheme("ace/theme/monokai");
    editor.session.setMode("ace/mode/php");
    editor.getSession().on('change', function(){
        tarea.value = editor.getSession().getValue();
    });
</script>
</body>
</html>

重要:为要编辑的文件设置正确的权限。

最新更新