文本文件的简单 PHP 编辑器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8226958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 04:12:18  来源:igfitidea点击:

Simple PHP editor of text files

phpeditorbackendhtml-editor

提问by Alehandro Darie

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.

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

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.

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

回答by hakre

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):

您创建一个 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($text) ?></textarea>
<input type="submit" />
<input type="reset" />
</form>

回答by Niels

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>

回答by Shamim Dewan

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>

回答by CodeCaster

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

你当时用什么谷歌?php 写文件给了我几百万次点击。

As in the manualfor fwrite():

手册中所述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 书籍并开始尝试。除了您想将文本字段(我的意思是 textarea?)发布到 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
}

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.

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

回答by James

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 oneand instead of emailing using mailcheck out the file functionsfrom PHP.net.

应用与教程相同的原则,而不是使用从 PHP.net 中mail检出文件函数发送电子邮件。

回答by Codecraft

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:

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

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

在“save.php”(或任何地方)上,您可以轻松查看从表单发送的信息:

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

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

要实际创建文件,请查看 PHP 中的 fopen/fwrite 命令,这是另一个简单示例:

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

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

回答by Brano

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.

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

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

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

echo "Text he wants to edit"; to:

echo "他要编辑的文字"; 到:

echo _t("S_Texthewantstoedit");

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.

stringmanager.com 负责其余的工作。您的客户可以在我们的在线应用程序中管理该特定文本区域,并随时随地同步。差点忘了说,它是完全免费的。

回答by Branyon

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

还没来得及完成它,尽可能简单,如果需要会添加更多。