使用 PHP/html 编辑 txt 文件


Editing a txt file with PHP/html

我目前正在使用此PHP脚本写入文本文件,然后使用另一个脚本从该文本文件中读取,但是如果我添加多个(同时更改文本文件的名称.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>"; 
} 
?> 

好的,我解决了自己的问题,这是一个愚蠢的问题,我忘了为字段指定唯一的名称:

<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>"; 
} 
?>