Forum Moderators: coopster

Message Too Old, No Replies

Very simple file editor?

php text editor

         

balzahk

2:50 pm on Mar 25, 2008 (gmt 0)

10+ Year Member



I would like to setup a very simple PHP script that can edit a text file on the server.

Let's say I have a file on my server, called message.txt and I want to make a script that can edit it.

So, on the page with a script, there would just be a single input box and a button that says "save". You type in whatever you want in the input box, hit save, and it automatically overwrites whatever was in the text file before.

I don't know how to write PHP. Are there any existing scripts that someone could show me?

I figured for security I would just make the directory containing the script protected with .htaccess.

Thanks for reading my question.

PHP_Chimp

8:27 pm on Mar 25, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You need to consider the security of what you are doing, however I will help with the php side of things (you can do the html yourself ;)
Depending on what you are doing with the information depends on how much security you need to think about. There is no security in this code, so this may or may not be what you are looking for...however I have tried to link to all of the functions that you will need for this sort of thing.
I have also put the code on the same page as the original form. Some people like this, others dont, so you will need to decide if this is for you.

I would suggest that you look for php and forms [w3schools.com] on the web, so that you have a better idea of what you are doing. As blindly using code is not a good idea (especially code written at 10pm ;)

html


<form method='post' action="<?php echo $_SERVER['SCRIPT_NAME'];?>">
<textarea id='text' cols='20' rows='10'>
Some code
</textarea>
<input type='submit' id='send' />
</form>

At the moment there is just a static "Some code" in the text area. Hopefully from the code below you will be able to work out how to read [uk2.php.net] the contents of a file and echo [uk2.php.net] it in that space, so that you can alter the contents.

the php


// assumes that 'some_file.txt' is the file you are writing to
if ($_POST['send']) { // check to see if the page has been posted
if ([url=http://uk2.php.net/manual/en/function.is-writable.php]is_writable[/url]('some_file.txt')) { // check to see if file is writable
$fh = [url=http://uk2.php.net/manual/en/function.fopen.php]fopen[/url]('some_file.txt', 'w'); // overwrites the old file, with the new data
[url=http://uk2.php.net/manual/en/function.fwrite.php]fwrite[/url]($fh,$_POST['text']);
[url=http://uk2.php.net/manual/en/function.fclose.php]fclose[/url]($fh);
[url=http://uk2.php.net/manual/en/function.header.php]header[/url]('Location: /some_page.php'); // redirect people to another page.
}
else { // file not writable
echo 'This file is not writable';
}
die('Ending for testing'); // this will stop the page getting echo'd again.
// not that you should see this as the redirect should mean you never get here...
}
// your html code under here

Hopefully that will get you started.

balzahk

7:19 pm on Mar 27, 2008 (gmt 0)

10+ Year Member



Thanks a LOT! I will play around with this and see how well I do. Thanks again!

balzahk

9:19 pm on Apr 4, 2008 (gmt 0)

10+ Year Member



Ok, I've been trying to get this to work without much success so far.

I decided to separate the HTML form and PHP script because I thought it would be easier.

Then, I adjusted the HTML for the form a little.

I changed "some_file.txt" to "../message.txt" since the file is in the parent directory. The code is as follows:

HTML Page:

<form method='post' action="edit.php">
<table>
<tr>
<td><strong>Enter New Message:</td>
<td><textarea id='message' cols='40' rows='1'></textarea></td>
</tr>
<tr>
<td colspan="2"><input type='submit' id='send' value="Save" /></td>
</tr>
</table>

PHP Script (of course, named "edit.php"):

<?php
// assumes that 'some_file.txt' is the file you are writing to
if ($_POST['send']) { // check to see if the page has been posted
if (is_writable('../message.txt')) { // check to see if file is writable
$fh = fopen('../message.txt', 'w'); // overwrites the old file, with the new data
fwrite($fh,$_POST['message']);
fclose($fh);
header('Location: /some_page.php'); // redirect people to another page.
}
else { // file not writable
echo 'This file is not writable';
}
die('Ending for testing'); // this will stop the page getting echo'd again.
// not that you should see this as the redirect should mean you never get here...
}
?>

However, when I ran it, I got this error:

Notice: Undefined index: send in C:\*snip*\edit.php on line 7

Line 7 is referring to: if ($_POST['send']) {

How can I fix this? Thanks.

coopster

7:30 pm on Apr 5, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



If the form variable has not been populated it will not be sent and therefore not defined as an index value in the $_POST superglobal. You need to at the very least check if it isset [php.net]
if (isset($_POST['send'])) { // check to see if the page has been posted

balzahk

11:39 pm on Apr 8, 2008 (gmt 0)

10+ Year Member



I went back replaced this line:

if ($_POST['send']) { // check to see if the page has been posted

With the line posted above:
if (isset($_POST['send'])) { // check to see if the page has been posted

Now, it doesn't give me an error message but nothing happens. I checked and the .txt file is writable, also.

MattAU

1:01 am on Apr 9, 2008 (gmt 0)

10+ Year Member



It sounds like 'send' isn't being posted... For debugging add an else statement to the end of the code -

else
{
die("'send' not posted");
}

That way you have a better idea of what's happening.

balzahk

5:52 pm on Apr 10, 2008 (gmt 0)

10+ Year Member



Once I added that to my code, I did get that message: "'send' not posted"

What am I doing incorrectly?

Here is the current code:

HTML:

<form method="post" action="edit.php">
<table>
<tr>
<td>Enter New Message:</td>
<td><textarea id='message' cols='40' rows='1'></textarea></td>
</tr>
<tr>
<td colspan="2"><input type='submit' id='send' value="Save" /></td>
</tr>
</table>
</form>
.
.
.
.
EDIT.PHP:

<?php
// assumes that 'some_file.txt' is the file you are writing to
if (isset($_POST['send'])) { // check to see if the page has been posted
if (is_writable('../message.txt')) { // check to see if file is writable
$fh = fopen('../message.txt', 'w'); // overwrites the old file, with the new data
fwrite($fh,$_POST['message']);
fclose($fh);
header('Location: /some_page.php'); // redirect people to another page.
}
else { // file not writable
echo 'This file is not writable';
}
die('Ending for testing'); // this will stop the page getting echo'd again.
// not that you should see this as the redirect should mean you never get here...
}
else
{
die("'send' not posted");
}
?>

MattAU

10:03 pm on Apr 10, 2008 (gmt 0)

10+ Year Member



id is not the right attribute... It's useful for CSS & Javscript, but not form processing.

<input type="submit" id="send" value="Save" />

Should be

<input type="submit" name="send" value="Save" />

Or

<input type="submit" id="send" name="send" value="Save" />

If you still want the id for css / js.

balzahk

10:09 pm on Apr 11, 2008 (gmt 0)

10+ Year Member



NICE, thanks. Looks like I got it working.

Thats to PHP_Chimp, coopster, and MattAU. BIG help!