Forum Moderators: coopster
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.
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>
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.
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.
if (isset($_POST['send'])) { // check to see if the page has been posted
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.
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");
}
?>