Need help with editing txt files with php

Hi guys.

I recently started to learn php. But sometimes i have problems :wink:
I have one today, and i hope someone can help me.

Im trying to create a php site. Where i can edit 3 text files (on my server) from that php site.
Its works fine for editing a single txt file.
But when i include the others, it makes some errors. Like: changing all 3 .txt files.

Heres my (edit text files page):

<?php include_once "eddauthor.php"; // Author include_once "eddemails.php"; // Emails include_once "eddcomment.php"; // Comment ?>

And heres ONE of my edit txt file scripts: (1 of 3)

Author:<?
if($_POST[‘Submit’]){
$open = fopen(“author.txt”,“w+”);
$text = $_POST[‘update’];
fwrite($open, $text);
fclose($open);
echo “File updated.
”;
echo “File:
”;
$file = file(“author.txt”);
foreach($file as $text) {
echo $text."
";
}
}else{
$file = file(“author.txt”);
echo “<form action=”".$PHP_SELF."" method=“post”>";
echo “<textarea Name=“update” cols=“10” rows=“1”>”;
foreach($file as $text) {
echo $text;
}
echo “”;
echo "<input name=“Submit” type=“submit” value=“Update” />\n

"; } ?>

Can anyone help me? i just want to be able to edit all boxes/ txt on the same site.

Best regards,
Mike / xan

heres another example:

Author:<?php

if($_POST['Submit']){
    $open = fopen("author.txt","w+");
    $text = $_POST['update'];
    fwrite($open, $text);
    fclose($open);
    echo "File updated.<br />";
    echo "File:<br />";
    $file = file("author.txt");
    foreach($file as $text) {
        echo $text."<br />";
    }

} else{
    $file = file("author.txt");
    echo "<form action=\"".$PHP_SELF."\" method=\"post\">";
    echo "<textarea Name=\"update\" cols=\"10\" rows=\"1\">";
    foreach($file as $text) {
        echo $text;
    }

    echo "</textarea>";
    echo "<input name=\"Submit\" type=\"submit\" value=\"Update\" />\n
"; }
?>

Emails:<?php

if($_POST['Submit']){
    $open = fopen("emails.txt","w+");
    $text = $_POST['update'];
    fwrite($open, $text);
    fclose($open);
    echo "File updated.<br />";
    echo "File:<br />";
    $file = file("emails.txt");
    foreach($file as $text) {
        echo $text."<br />";
    }

} else{
    $file = file("emails.txt");
    echo "<form action=\"".$PHP_SELF."\" method=\"post\">";
    echo "<textarea Name=\"update\" cols=\"10\" rows=\"1\">";
    foreach($file as $text) {
        echo $text;
    }

    echo "</textarea>";
    echo "<input name=\"Submit\" type=\"submit\" value=\"Update\" />\n
</form>";
}

?>
body { font-family: verdana, arial; background:#FF9900; font-size:11px; }

Comment:<?php

if($_POST['Submit']){
    $open = fopen("comments.txt","w+");
    $text = $_POST['update'];
    fwrite($open, $text);
    fclose($open);
    echo "File updated.<br />";
    echo "File:<br />";
    $file = file("comments.txt");
    foreach($file as $text) {
        echo $text."<br />";
    }

} else{
    $file = file("comments.txt");
    echo "<form action=\"".$PHP_SELF."\" method=\"post\">";
    echo "<textarea Name=\"update\" cols=\"50\" rows=\"10\">";
    foreach($file as $text) {
        echo $text;
    }

    echo "</textarea>";
    echo "<input name=\"Submit\" type=\"submit\" value=\"Update\" />\n
"; }
?>

Hi there,

I had a quick read through and quickly threw this together, let me know if it helps you and/or how it doesn’t.

<?php
if(isset($_POST['form_sub']))
{
   $handle = fopen($_POST['file'].".txt","w+");
   fwrite($handle,$_POST['text']);
   fclose($handle);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
	<title>File editing</title>
</head>
<body>
	<form action="" method="post">
		<select name="file">
			<option value="author">author.txt</option>
			<option value="emails">emails.txt</option>
			<option value="comments">comments.txt</option>
		</select>
		<textarea cols="40" rows="30" name="text"></textarea>
		<input type="submit" name="form_sub" />
	</form>
</body>
</html>

Obviously you’ll want to add some messages etc in the PHP and maybe some error checking. But this basis should help you.

Sponsor our Newsletter | Privacy Policy | Terms of Service