使用PHP / html编辑txt文件

时间:2013-09-14 23:57:31

标签: php html

我目前正在使用这个PHP脚本写入文本文件,然后另一个用于读取该文本文件,但是如果我添加多个这些文件(同时更改textfile.txt的名称以写入多个文本)文件,它更新了两个文件与在一个字段中更新的任何内容。我怎么能够允许这两个文件写入多个文本文件而不复制并将它们粘贴到单独的页面。

用于编辑/更新的PHP:

<? 
if($_POST['Submit']){ 
$open = fopen("textfile.txt","w+"); 
$text = $_POST['update']; 
fwrite($open, $text); 
fclose($open); 
echo "File updated.<br />";  
echo "File:<br />"; 
$file = file("textfile.txt"); 
foreach($file as $text) { 
echo $text."<br />"; 
} 
}else{ 
$file = file("textfile.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 
</form>"; 
} 
?>

我意识到我没有发布我的非工作示例,这里是:

<h2>content1</h2>
<? 
if($_POST['Submit']){ 
$open = fopen("../content.txt","w+"); 
$text = $_POST['update']; 
fwrite($open, $text); 
fclose($open); 
echo "File updated.<br />";  
echo "File:<br />"; 
$file = file("../content.txt"); 
foreach($file as $text) { 
echo $text."<br />"; 
} 
}else{ 
$file = file("../content.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 
</form>"; 
} 
?> 
<br />
<h2>content2</h2>
<? 
if($_POST['Submit']){ 
$open = fopen("../content2.txt","w+"); 
$text = $_POST['update']; 
fwrite($open, $text); 
fclose($open); 
echo "File updated.<br />";  
echo "File:<br />"; 
$file = file("../content2.txt"); 
foreach($file as $text) { 
echo $text."<br />"; 
} 
}else{ 
$file = file("../content2.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 
</form>"; 
} 
?> 

1 个答案:

答案 0 :(得分:3)

好的,我解决了自己的问题,这是一个愚蠢的问题,我忘了给这些字段赋予独特的名字:

<h2>content1</h2>
<? 
if($_POST['Submit']){ 
$open = fopen("../content.txt","w+"); 
$text = $_POST['update']; 
fwrite($open, $text); 
fclose($open); 
echo "File updated.<br />";  
echo "File:<br />"; 
$file = file("../content.txt"); 
foreach($file as $text) { 
echo $text."<br />"; 
} 
}else{ 
$file = file("../content.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 
</form>"; 
} 
?> 
<br />
<h2>content2</h2>
<? 
if($_POST['Submit2']){ 
$open = fopen("../content2.txt","w+"); 
$text = $_POST['update2']; 
fwrite($open, $text); 
fclose($open); 
echo "File updated.<br />";  
echo "File:<br />"; 
$file = file("../content2.txt"); 
foreach($file as $text) { 
echo $text."<br />"; 
} 
}else{ 
$file = file("../content2.txt"); 
echo "<form action=\"".$PHP_SELF."\" method=\"post\">"; 
echo "<textarea Name=\"update2\" cols=\"50\" rows=\"10\">"; 
foreach($file as $text) { 
echo $text; 
}  
echo "</textarea>"; 
echo "<input name=\"Submit2\" type=\"submit\" value=\"Update\" />\n 
</form>"; 
} 
?>