php - 文本文件的简单 PHP 编辑器

标签 php editor backend html-editor

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

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

最佳答案

您创建一个 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>

关于php - 文本文件的简单 PHP 编辑器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8226958/

相关文章:

php - 使用 JQuery 将 .php 文件加载到 div 中

XML 编辑器,如 Word 2003

python - 如何使用自定义 Django 后端?

php - DateTime::createFromFormat 如何捕获特定的日期时间?

php - 向用户显示年龄检查表,而不是机器人 - SEO 安全吗?

php - Composer Yii2 Bower : The file or directory to be published does not exist: C:\myProject\vendor\bower/jquery/dist

linux - 如何在Vim中使用help命令查看.text文件?

java - 用于启用/禁用 JAVA 断点的 eclipse 快捷方式

node.js - 使用 NodeJs 和 Express 部署 Web 应用程序

ios - 当我在控制台中引入 pod install 时,出现错误 (NoMethodError - undefined method `to_ary' for #<Pod::Specification name ="Parse">)