Simple PHP editor of text files

倖福魔咒の 提交于 2019-11-28 05:58:11

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

<?php

// configuration
$url = 'http://domain.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>

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>

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.

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.

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!

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!