View previous topic :: View next topic |
Author |
Message |
cmxflash -
Joined: 11 Dec 2004 Posts: 872
|
Posted: Tue May 31, 2005 4:26 pm Post subject: PHP text-editor? |
|
|
Does anybody know a PHP-script that lets you edit a TXT-file on the server? |
|
Back to top |
|
 |
MonkeyNation -
Joined: 05 Feb 2005 Posts: 921 Location: Cardiff
|
Posted: Tue May 31, 2005 8:47 pm Post subject: Re: PHP text-editor? |
|
|
cmxflash wrote: | Does anybody know a PHP-script that lets you edit a TXT-file on the server? |
Code: |
<?php
/////////////////////////////
$file = "editme.file";//////
/////////////////////////////
echo "<html>\n";
echo "<head>\n";
echo "</head>\n";
echo "<body>\n";
if (isset($_POST['contents'])) {
if (!file_exists($file)) {
touch($file);
}
$open = fopen($file, 'w+');
fwrite($open, $_POST['contents']) or die("Error writing file.");
fclose($open);
echo "<h1>File edited!</h1><br>\n";
}
echo "<form action=\"".$_SERVER['PHP_SELF']."\" method=\"POST\" name=\"writefile\">\n";
echo "<textarea name=\"contents\" cols=\"60\" rows=\"8\">\n";
if (file_exists($file)) {
echo str_replace("<", "<", str_replace(">", ">", implode("", file($file))))."\n";
}
echo "</textarea><br>\n";
echo "<input type=\"submit\" value=\"Edit File\">\n";
echo "</form>\n";
echo "</body>\n";
echo "</html>\n";
?>
|
Hopefully thats ok, there may be errors as I just typed it out though. _________________
 |
|
Back to top |
 |
 |
cmxflash -
Joined: 11 Dec 2004 Posts: 872
|
Posted: Tue May 31, 2005 11:16 pm Post subject: Re: PHP text-editor? |
|
|
I found a little bug, if you write in something and press the submit-button 5 times, it makes 5 new lines. Anyway, I've fixed it:
Code: | <?php
$file = "test.txt";
echo "<html>\n";
echo "<head>\n";
echo "</head>\n";
echo "<body>\n";
if (isset($_POST['contents'])) {
if (!file_exists($file)) {
touch($file);
}
$open = fopen($file, 'w+');
fwrite($open, $_POST['contents']) or die("Cannto write the file");
fclose($open);
echo "The file was written!<br>\n";
}
echo "<form action=\"".$_SERVER['PHP_SELF']."\" method=\"POST\" name=\"writefile\">";
echo "<textarea name=\"contents\" cols=\"60\" rows=\"8\">";
if (file_exists($file)) {
echo str_replace("<", "<", str_replace(">", ">", implode("", file($file))))."";
}
echo "</textarea><br>\n";
echo "<input type=\"submit\" value=\"Spara\">\n";
echo "</form>\n";
echo "</body>\n";
echo "</html>\n";
?> |
|
|
Back to top |
|
 |
MonkeyNation -
Joined: 05 Feb 2005 Posts: 921 Location: Cardiff
|
Posted: Wed Jun 01, 2005 12:15 am Post subject: |
|
|
Also, I just noticed the forum replaced my ASCII values, as it should.
On the str_replaces, change the second character to an ascii value of it, or look at the source of the forum page to see it. _________________
 |
|
Back to top |
 |
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|