我已经为客户开发了一个网站,他希望能够在一个后端类型的解决方案编辑主页面的一小部分。因此,作为解决方案,我想添加一个非常基本的编辑器(domain.com/backend/editor.php),当您访问它时,它将有一个包含代码的文本字段和一个保存按钮。它将编辑的代码将被设置为TXT文件。
我假设这样的事情很容易用PHP编写,但是google这次没有帮助我,所以我希望这里有人能给我指明正确的方向。请注意,我没有在PHP编程的经验,只有HTML和基本的javascript,所以请在您提供的任何答复彻底。
发布于 2011-11-22 20:45:30
您创建一个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>
发布于 2011-11-22 20:43:59
读取文件的步骤:
<?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>
发布于 2011-11-22 20:42:42
您基本上是在寻找一个类似于联系人表单或类似表单的概念。
应用像this one这样的教程中的相同原则,而不是使用mail
发送电子邮件,请查看PHP.net的file functions。
https://stackoverflow.com/questions/8226958
复制相似问题