使用 PHP/html 编辑 txt 文件 [英] Editing a txt file with PHP/html

查看:33
本文介绍了使用 PHP/html 编辑 txt 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm currently using this PHP script to write to a text file, then another to read from that text file, however if I add more than one of these (while changing the name of textfile.txt to write to multiple text files, it updated both files with whatever is updated in the one field. How would I be able to allow this to two write to multiple text files without copy and pasting them to separate pages.

用于编辑/更新的 PHP:

THE PHP used to edit/update:

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

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

I realize I didn't post my nonworking example, here it is:

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

推荐答案

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

OK I solved my own problem, it was a silly issue, I forgot to give unique names to the fields:

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

这篇关于使用 PHP/html 编辑 txt 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆