如何使用PHP从HTML textarea保存TXT文件? [英] How can I save TXT Files from a HTML textarea, using PHP?
问题描述
这是我到目前为止的内容。
我已经获得了一些部分的工作,但是在做出一些更改后我得到了更多的错误,我认为这会有所帮助。
我正在尝试完成此操作
<!DOCTYPE HTML>
< html>
< body>
<?php
if(isset($ _ POST)){
//保存文件
$ file = fopen($ _ POST ['filename']&.txt , R +);
$ text = $ _POST [textdata];
file_put_contents($ file,$ text);
fclose($ file);
//打开文件
$ file = fopen($ _ POST ['filename']&.txt,r)或退出(Unable to open file。);
while(!feof($ file)){
echo fgets($ file)。 < br />;
}
fclose($ file);
}
echo'
< form name =savefilemethod =postaction ='。$ _SERVER ['PHP_SELF']。'>
档案名称:< input type =textname =filenamevalue =>< br />
< textarea rows =20cols =100name =textdata>< / textarea>< br />
< input type =submitname =submitvalue =保存文本到服务器>
< / form>
< br />< hr style =width:100%; height:4px;>< br />
< form name =openfilemethod =postaction ='。$ _SERVER ['PHP_SELF']。'>
打开文件:< input type =textname =filenamevalue =>
< input type =submitname =submitvalue =提交文件请求>
< / form>';
?>
< / body>
< html>
如果唯一的方法是让它重定向到一个php页面,然后发回它,那就是好,但我不知道如何做到这一点,(即使它可能很简单)
感谢您提供任何帮助或建议!
-Jake
首先,非常感谢sooo的帮助!
我真的很感激那些经验丰富的人可以偶尔帮助新手。
在花了一些时间修改我的代码并提出了一些建议后,我终于完成了它的工作! :D
我会在这里留下最终的代码,以防其他人在类似问题上绊倒这个主题。
<!DOCTYPE HTML>
< html>
< body style =background-color:rgb(225,225,225)>
< form name =savefilemethod =postaction =>
档案名称:< input type =textname =filenamevalue =>< br />
< textarea rows =16cols =100name =textdata>< / textarea>< br />
< input type =submitname =submitsavevalue =保存文本到服务器>
< / form>
< br />< hr style =background-color:rgb(150,150,150); color:rgb(150,150,150); width:100%; height:4px;>< br />
< form name =openfilemethod =postaction =>
打开文件:< input type =textname =filenamevalue =>
< input type =submitname =submitopenvalue =提交文件请求>
< / form>
< br />< hr style =background-color:rgb(150,150,150); color:rgb(150,150,150); width:100%; height:4px;>< br />
文件内容:< br /> $($ _ $ POST)){
if($ _POST ['submitsave'] ==Save Text to Server&&!empty _POST ['filename'])){
if(!file_exists($ _ POST ['filename']。.txt)){
$ file = tmpfile();
}
$ file = fopen($ _ POST ['filename']。.txt,a +);
while(!feof($ file)){
$ old = $ old。与fgets($文件)。 < br />;
}
$ text = $ _POST [textdata];
file_put_contents($ _ POST ['filename']。.txt,$ old。$ text);
fclose($ file); ($ _BOST ['submitopen'] ==提交文件请求){
if(!file_exists($ _ POST ['filename']。)。 txt)){
exit(错误:文件不存在。);
}
$ file = fopen($ _ POST ['filename']。.txt,r);
while(!feof($ file)){
echo fgets($ file)。 < br />;
}
fclose($ file);
}
}
?>
< / body>
< / html>
希望这有助于您!
--Jake
I am trying to make a text file storage system for my website.
Here is what I have so far.
I have gotten some parts to work, but am getting many more errors after making a few changes I thought would help.
I am trying to accomplish this task without changing pages or url.
<!DOCTYPE HTML>
<html>
<body>
<?php
if (isset($_POST)){
//Save File
$file = fopen($_POST['filename'] & ".txt","r+");
$text = $_POST["textdata"];
file_put_contents($file, $text);
fclose($file);
//Open File
$file = fopen($_POST['filename'] & ".txt", "r") or exit("Unable to open file.");
while(!feof($file)){
echo fgets($file). "<br />";
}
fclose($file);
}
echo '
<form name="savefile" method="post" action="' . $_SERVER['PHP_SELF'] . '">
File Name: <input type="text" name="filename" value=""><br/>
<textarea rows="20" cols="100" name="textdata"></textarea><br/>
<input type="submit" name="submit" value="Save Text to Server">
</form>
<br/><hr style="width: 100%; height: 4px;"><br/>
<form name="openfile" method="post" action="' . $_SERVER['PHP_SELF'] . '">
Open File: <input type="text" name="filename" value="">
<input type="submit" name="submit" value="Submit File Request">
</form>';
?>
</body>
<html>
If the only way is to have it redirect to a php page, then send it back, that is fine, but I have no clue how to do that, (even though its probably A LOT simpler)
Thanks for any help or advice you can provide me!
-Jake
First of all, thanks sooo much for the help!
I really appreciate it when those people who are more experienced can help out a newbie once in a while.
After spending some time tinkering with my code with some of the suggestions from you, I have finally gotten it to work! :D
I'll leave the final code here in case anyone else stumbles upon this topic with a similar issue.
<!DOCTYPE HTML>
<html>
<body style="background-color: rgb(225,225,225)">
<form name="savefile" method="post" action="">
File Name: <input type="text" name="filename" value=""><br/>
<textarea rows="16" cols="100" name="textdata"></textarea><br/>
<input type="submit" name="submitsave" value="Save Text to Server">
</form>
<br/><hr style="background-color: rgb(150,150,150); color: rgb(150,150,150); width: 100%; height: 4px;"><br/>
<form name="openfile" method="post" action="">
Open File: <input type="text" name="filename" value="">
<input type="submit" name="submitopen" value="Submit File Request">
</form>
<br/><hr style="background-color: rgb(150,150,150); color: rgb(150,150,150); width: 100%; height: 4px;"><br/>
File Contents:<br/>
<?php
if (isset($_POST)){
if ($_POST['submitsave'] == "Save Text to Server" && !empty($_POST['filename'])) {
if(!file_exists($_POST['filename'] . ".txt")){
$file = tmpfile();
}
$file = fopen($_POST['filename'] . ".txt","a+");
while(!feof($file)){
$old = $old . fgets($file). "<br />";
}
$text = $_POST["textdata"];
file_put_contents($_POST['filename'] . ".txt", $old . $text);
fclose($file);
}
if ($_POST['submitopen'] == "Submit File Request") {
if(!file_exists($_POST['filename'] . ".txt")){
exit("Error: File does not exist.");
}
$file = fopen($_POST['filename'] . ".txt", "r");
while(!feof($file)){
echo fgets($file). "<br />";
}
fclose($file);
}
}
?>
</body>
</html>
Hope this helps!
-Jake
这篇关于如何使用PHP从HTML textarea保存TXT文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!