简单的PHP文本文件编辑器

Ale*_*rie 18 php editor backend html-editor

我为客户开发了一个站点,他希望能够在后端类型的解决方案中编辑主页的一小部分.所以作为一个解决方案,我想添加一个非常基本的编辑器(domain.com/backend/editor.php),当你访问它时,它将有一个包含代码和保存按钮的文本字段.它将编辑的代码将设置为TXT文件.

我认为这样的东西很容易在PHP中编码,但谷歌这次没有帮助我所以我希望这里可能有人会指出我正确的方向.请注意,我没有PHP编程经验,只有HTML和基本的javascript,所以请在您提供的任何回复中彻底.

hak*_*kre 37

您创建一个HTML表单来编辑文本文件的内容.如果它已提交,则更新文本文件(并再次重定向到表单以防止F5/Refresh警告):

<?php

// configuration
$url = 'http://example.com/backend/editor.php';
$file = '/path/to/txt/file';

// check if form has been submitted
if (isset($_POST['text']))
{
    // save the text contents
    file_put_contents($file, $_POST['text']);

    // redirect to form again
    header(sprintf('Location: %s', $url));
    printf('<a href="%s">Moved</a>.', htmlspecialchars($url));
    exit();
}

// read the textfile
$text = file_get_contents($file);

?>
<!-- HTML form -->
<form action="" method="post">
<textarea name="text"><?php echo htmlspecialchars($text) ?></textarea>
<input type="submit" />
<input type="reset" />
</form>
Run Code Online (Sandbox Code Playgroud)


Nie*_*els 5

要读取文件:

<?php
    $file = "pages/file.txt";
    if(isset($_POST))
    {
        $postedHTML = $_POST['html']; // You want to make this more secure!
        file_put_contents($file, $postedHTML);
    }
?>
<form action="" method="post">
    <?php
    $content = file_get_contents($file);
    echo "<textarea name='html'>" . htmlspecialchars($content) . "</textarea>";
    ?>
    <input type="submit" value="Edit page" />
</form>
Run Code Online (Sandbox Code Playgroud)