Page 1 of 1
editing a .txt file with php
Posted: Fri Jan 23, 2004 9:21 am
by mccommunity
I have a .txt file on my server and I need to somehow build an interface that will open this and let the user type in new data then resave it with the changes. How would I do this?
Thanks
Posted: Fri Jan 23, 2004 9:22 am
by twigletmac
confusing
Posted: Fri Jan 23, 2004 10:19 am
by mccommunity
This is a bit confusing to me. Does anyone have a script I can look at to see how this is done.
I tried this
Posted: Fri Jan 23, 2004 10:48 am
by mccommunity
I tried to use the code below that I came up with. This does not work it give me errors, I did set the permissions on the file and directory to chmod 777:
Warning: fwrite(): supplied argument is not a valid stream resource in /usr/www/users/development/php/text editor/test.php on line 40
Warning: fclose(): supplied argument is not a valid stream resource in /usr/www/users/development/php/text editor/test.php on line 41
<html>
<?php
$filename="text.txt";
// Gets webpage into a string
$html = implode('', file('text.txt'));
echo $html;
?>
<body>
<form name="form1" method="post" action="test.php">
<textarea name="newfile" cols="30" rows="10"><? echo $html; ?></textarea>
<input type="submit" name="submit" value="submit">
</form>
</html>
<?
if ($submit)
{
$file = "$filename";
$fh = fopen($file, 'w+');
fwrite($fh, $newfile);
fclose($fh);
}
?>
Posted: Fri Jan 23, 2004 2:23 pm
by DuFF
A few problems with that code, check out my comments:
Code: Select all
<html>
<?php
$filename="text.txt";
// Gets webpage into a string
$html = implode('', file('text.txt'));
echo $html;
?>
<body>
<form name="form1" method="post" action="test.php">
<textarea name="newfile" cols="30" rows="10"><? echo $html; ?></textarea>
<input type="submit" name="submit" value="submit">
</form>
</html>
<?
if (isset($_POST["submit"])) // notice the addition of $_POST! read below for more info
{
// $file = "$filename"; what is the point of this line? You've already got the name in a variable
$handle = fopen($filename, 'w+');
fwrite($handle, $newfile);
fclose($handle);
}
?>
If you don't have register_globals enabled (which you shouldn't), the if($submit) is not the correct way to write it. Because you are passing the form info using the POST method, you want to use the
post superglobal.