简单的 PHP 文本文件编辑器

发布于 2024-12-17 05:29:10 字数 270 浏览 7 评论 0原文

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

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

I have developed a site for a client and he wants to be able to edit a small part of the main page in a backend type of solution. So as a solution, I want to add a very basic editor (domain.com/backend/editor.php) that when you visit it, it will have a textfield with the code and a save button. The code that it will edit will be set to a TXT file.

I would presume that such thing would be easy to code in PHP but google didn't assist me this time so I am hoping that there might be someone here that would point me to the right direction. Note that I have no experience in PHP programming, only HTML and basic javascript so please be thorough in any reply that you provide.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

南街九尾狐 2024-12-24 05:29:10

您创建一个 HTML 表单来编辑文本文件的内容。如果提交了,您可以更新文本文件(并再次重定向到表单以防止出现 F5/刷新警告):

<?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((string)$text); ?></textarea>
<input type="submit" />
<input type="reset" />
</form>

You create a HTML form to edit the text-file's content. In case it get's submitted, you update the text-file (and redirect to the form again to prevent F5/Refresh warnings):

<?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((string)$text); ?></textarea>
<input type="submit" />
<input type="reset" />
</form>
陌伤ぢ 2024-12-24 05:29:10

读取文件:

<?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>

To read the file:

<?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>
醉生梦死 2024-12-24 05:29:10

您基本上是在寻找与联系表单或类似内容类似的概念。

应用此教程中的相同原则,而不是使用 mail 发送电子邮件 查看 PHP.net 中的文件函数

You're basically looking for a similar concept to that of a contact-form or alike.

Apply the same principles from a tutorial like this one and instead of emailing using mail check out the file functions from PHP.net.

全部不再 2024-12-24 05:29:10

那你谷歌了什么? php 写入文件 给我带来了几百万次点击。

正如 fwrite()手册 中所示:

<?php
$fp = fopen('data.txt', 'w');
fwrite($fp, '1');
fwrite($fp, '23');
fclose($fp);

// the content of 'data.txt' is now 123 and not 23!
?>

但是说实话,你应该首先拿起一本 PHP 书籍并开始尝试。除了要将文本字段(我的意思是文本区域?)发布到 TXT 文件之外,您没有发布任何单一要求。这样就可以了:

<?php
if ($_SERVER['REQUEST_METHOD'] == "POST")
{
    $handle = fopen("home.txt", 'w') or die("Can't open file for writing.");
    fwrite($fh, $_POST['textfield']);
    fclose($fh);
    echo "Content saved.";
}
else
{
    // Print the form
    ?>
    <form method="post">
        <textarea name="textfield"></textarea>
        <input type="submit" />
    </form>
    <?php
}

请注意,这与您的描述完全匹配。它在打印表单时不会读取文件(因此每次您想要编辑文本时,您都必须从头开始),它不会检查输入的任何内容(您是否希望用户能够发布 HTML ?),它没有安全检查(每个人都可以访问它并更改文件),并且它不会读取文件以显示在您想要的页面上。

What did you Google on then? php write file gives me a few million hits.

As in the manual for fwrite():

<?php
$fp = fopen('data.txt', 'w');
fwrite($fp, '1');
fwrite($fp, '23');
fclose($fp);

// the content of 'data.txt' is now 123 and not 23!
?>

But to be honest, you should first pick up a PHP book and start trying. You have posted no single requirement, other than that you want to post a textfield (textarea I mean?) to a TXT file. This will do:

<?php
if ($_SERVER['REQUEST_METHOD'] == "POST")
{
    $handle = fopen("home.txt", 'w') or die("Can't open file for writing.");
    fwrite($fh, $_POST['textfield']);
    fclose($fh);
    echo "Content saved.";
}
else
{
    // Print the form
    ?>
    <form method="post">
        <textarea name="textfield"></textarea>
        <input type="submit" />
    </form>
    <?php
}

Note that this exactly matches your description. It doesn't read the file when printing the form (so every time you want to edit the text, you have to start from scratch), it does not check the input for anything (do you want the user to be able to post HTML?), it has no security check (everyone can access it and alter the file), and in no way it reads the file for display on the page you want.

燃情 2024-12-24 05:29:10

首先要做的是捕获信息,最简单的方法是使用带有 TEXTAREA 的 HTML 表单:

<form method='post' action='save.php'>
  <textarea name='myTextArea'></textarea>
  <button type='submit'>Go</button>
</form>

的信息:

<?php
  echo $_POST['myTextArea']
?>

在“save.php”(或任何地方)上,您可以轻松地看到从表单发送 创建一个文件,看看 PHP 中的 fopen/fwrite 命令,另一个简单的例子:

<?php 
  $handle = fopen("myFile.txt","w");
  fwrite($handle,$_POST['myTextArea'];
  fclose($handle);
?>

警告:这是一个极其简单的答案!您可能想要保护您的表单和文件,或者做一些不同的事情......以上所有要做的就是将表单中发布的内容准确写入文件。如果您想指定不同的文件名、覆盖、追加、检查不良内容/垃圾邮件等,那么您需要做更多的工作。

如果您有一个可公开访问的编辑器并将内容发布到网页,那么垃圾邮件防护是一项明确的要求,否则您会后悔的!

如果您对学习 PHP 不感兴趣,那么您应该考虑找一个专业的开发人员来为您处理任何编码工作!

First thing to do is capture the information, the simplest way to do this would be the use of a HTML Form with a TEXTAREA:

<form method='post' action='save.php'>
  <textarea name='myTextArea'></textarea>
  <button type='submit'>Go</button>
</form>

On 'save.php' (or wherever) you can easily see the information sent from the form:

<?php
  echo $_POST['myTextArea']
?>

To actually create a file, take a look at the fopen/fwrite commands in PHP, another simplistic example:

<?php 
  $handle = fopen("myFile.txt","w");
  fwrite($handle,$_POST['myTextArea'];
  fclose($handle);
?>

WARNING: This is an extremely simplistic answer! You will perhaps want to protect your form and your file, or do some different things.... All the above will do is write EXACTLY what was posted in the form to a file. If you want to specify different filenames, overwrite, append, check for bad content/spam etc then you'll need to do more work.

If you have an editor that is publicly accessible and publishes content to a web page then spam protection is a DEFINITE requirement or you will come to regret it!

If you aren't interested in learning PHP then you should think about getting a professional developer to take care of any coding work for you!

淡淡的优雅 2024-12-24 05:29:10

我有类似的需求,因此我们创建了一个名为 stringmanager.com 的客户端友好解决方案,我们在所有项目和 CMS 无效的地方使用该解决方案。

从你的角度来看,你只需要在代码中标记字符串,即 from:

echo "Text he要编辑";
至:

echo _t("S_Texthewantstoedit");

stringmanager.com 负责剩下的事情。您的客户可以在我们的在线应用程序中管理该特定文本区域,并在他想要的任何地方进行同步。差点忘了提,它是完全免费的。

I had a similar need so we created a client-friendly solution called stringmanager.com we use on all our projects and places where CMS is not effective.

From your side, you just need to tag string in the code, i.e. from:

echo "Text he wants to edit";
to:

echo _t("S_Texthewantstoedit");

stringmanager.com takes care about the rest. Your client can manage that particular text area in our online application and sync wherever he wants. Almost forgot to mention, it is completely free.

很酷不放纵 2024-12-24 05:29:10

可以使用这行代码:

    <form action="" method="post">
    <textarea id="test" name="test" style="width:100%; height:50%;"><? echo "$test"; ?></textarea>
    <input type="submit" value="submit">
    </form>

Can use this line of code :

    <form action="" method="post">
    <textarea id="test" name="test" style="width:100%; height:50%;"><? echo "$test"; ?></textarea>
    <input type="submit" value="submit">
    </form>
无敌元气妹 2024-12-24 05:29:10
<?php
$file = "127.0.0.1/test.html";
$test = file_get_contents('1.jpg', 'a');
if (isset($_POST['test'])) {
file_put_contents($file, $_POST["test"]);
};
?>
<form action="" method="post">
<textarea id="test" name="test" style="width:100%; height:50%;"><? echo "$test"; ?></textarea>
<input type="submit" value="submit">
</form>

还没时间完成,最简单的,如果需要的话会添加更多。

<?php
$file = "127.0.0.1/test.html";
$test = file_get_contents('1.jpg', 'a');
if (isset($_POST['test'])) {
file_put_contents($file, $_POST["test"]);
};
?>
<form action="" method="post">
<textarea id="test" name="test" style="width:100%; height:50%;"><? echo "$test"; ?></textarea>
<input type="submit" value="submit">
</form>

Haven't had time to finish it, simplest possible, will add more if wanted.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文