Php 下载html文本区域内容作为文件

Php 下载html文本区域内容作为文件,php,html,wordpress,Php,Html,Wordpress,我正在做一个项目,要求在wordpress网站上下载一个textarea内容作为.txt文件 在搜索了前面的问题后,我得到了下面的代码,该代码标记为正确答案 <html> <body> <form action="#" method="post"> <textarea name="text" rows="20" cols="100"></textarea> <input type="submit" value="submit"&g

我正在做一个项目,要求在wordpress网站上下载一个textarea内容作为.txt文件

在搜索了前面的问题后,我得到了下面的代码,该代码标记为正确答案

<html>
<body>
<form action="#" method="post">
<textarea name="text" rows="20" cols="100"></textarea>
<input type="submit" value="submit">Download Text</input>
</form>

<?PHP

if(isset($_POST['submit']))
 {   

$text = $_POST['text'];
print ($text);

$filename = 'test.txt';
$string = $text;

$fp = fopen($filename, "w");
fwrite($fp, $string);
fclose($fp);

header('Content-disposition: attachment; filename=test.txt');
header('Content-type: application/txt');
readfile('test.txt');

 }

?>

</body>
</html>

下载文本

但对我来说,这并不是创建一个.txt文件。请帮助我识别问题

请尝试以下代码,这将对您有所帮助:

<?php  ob_start();?>
<html>
<body>
<?php

     if(isset($_POST['tarea'])){
     $filename = 'test.txt';
     $data = $_POST['tarea']; 
     header("Cache-Control: public");
     header("Content-Description: File Transfer");
     header("Content-Disposition: attachment; filename=$filename");
     header("Content-Type: application/octet-stream; "); 
     header("Content-Transfer-Encoding: binary");
     echo $data;
     exit;
}
?>
<form action="" method="post">
<textarea name="tarea"></textarea>
<input type="submit">     
</form>
</body>
</html>

尝试以下代码,这将帮助您:

<?php  ob_start();?>
<html>
<body>
<?php

     if(isset($_POST['tarea'])){
     $filename = 'test.txt';
     $data = $_POST['tarea']; 
     header("Cache-Control: public");
     header("Content-Description: File Transfer");
     header("Content-Disposition: attachment; filename=$filename");
     header("Content-Type: application/octet-stream; "); 
     header("Content-Transfer-Encoding: binary");
     echo $data;
     exit;
}
?>
<form action="" method="post">
<textarea name="tarea"></textarea>
<input type="submit">     
</form>
</body>
</html>

修改了代码中的一点点内容

首先把你的php代码

 <?php

    if(isset($_POST['submit']))
     {   

    $text = $_POST['text'];
    print ($text);

    $filename = 'test.txt';
    $string = $text;

    $fp = fopen($filename, "w");
    fwrite($fp, $string);
    fclose($fp);

    header('Content-disposition: attachment; filename=test.txt');
    header('Content-type: application/txt');
    readfile('test.txt');
    die; //modified code 
     }
    ?>

之后,把你的html代码

<html>
<body>
<form action="#" method="post">
<textarea name="text" rows="20" cols="100"></textarea>
<input type="submit" value="submit" name="submit">Download Text</input>
</form>

下载文本

在提交按钮中添加name属性。

修改了代码中的一点点内容

首先把你的php代码

 <?php

    if(isset($_POST['submit']))
     {   

    $text = $_POST['text'];
    print ($text);

    $filename = 'test.txt';
    $string = $text;

    $fp = fopen($filename, "w");
    fwrite($fp, $string);
    fclose($fp);

    header('Content-disposition: attachment; filename=test.txt');
    header('Content-type: application/txt');
    readfile('test.txt');
    die; //modified code 
     }
    ?>

之后,把你的html代码

<html>
<body>
<form action="#" method="post">
<textarea name="text" rows="20" cols="100"></textarea>
<input type="submit" value="submit" name="submit">Download Text</input>
</form>

下载文本

在submit按钮中添加name属性。

您的代码实际上在服务器上创建了一个文件,之后不会被删除。如果代码失败,则可能是您没有在服务器上写入的权限。您可以将代码精简为以下内容,以便在此过程中不生成任何文件:

<?php
if(isset($_POST['text']))
 {
   header('Content-disposition: attachment; filename=test.txt');
   header('Content-type: application/txt');
   echo $_POST['text'];
   exit; //stop writing
 }
?>
<html>
<body>
<form action="" method="post">
<textarea name="text" rows="20" cols="100"></textarea>
<input type="submit" value="submit" name="submit">Download Text</input>
</form>

</body>
</html>
将表单添加到某个模板文件中或在帖子的HTML编辑器中:

<form action="" method="post">
  <textarea name="text_to_download" rows="20" cols="100"></textarea>
  <input type="submit" value="submit" name="submit">Download Text</input>
</form>

您的代码实际上在服务器上创建了一个文件,之后不会被删除。如果代码失败,则可能是您没有在服务器上写入的权限。您可以将代码精简为以下内容,以便在此过程中不生成任何文件:

<?php
if(isset($_POST['text']))
 {
   header('Content-disposition: attachment; filename=test.txt');
   header('Content-type: application/txt');
   echo $_POST['text'];
   exit; //stop writing
 }
?>
<html>
<body>
<form action="" method="post">
<textarea name="text" rows="20" cols="100"></textarea>
<input type="submit" value="submit" name="submit">Download Text</input>
</form>

</body>
</html>
将表单添加到某个模板文件中或在帖子的HTML编辑器中:

<form action="" method="post">
  <textarea name="text_to_download" rows="20" cols="100"></textarea>
  <input type="submit" value="submit" name="submit">Download Text</input>
</form>

Try at Try at:it giving error
警告:单击下载按钮后无法修改标题信息-标题已发送
在我的示例中,您是否收到警告?调用ob_start()在脚本顶部缓冲输出:it giving error
警告:单击下载按钮后无法修改标题信息-标题已发送
在我的示例中,是否收到警告?调用ob_start()在脚本顶部缓冲输出。此代码给我错误`警告:无法修改标题信息-标题已由发送(输出开始于C:\Users\aratnan\Desktop\wordpress\InstantWP\U 4.3\iwpserver\htdocs\wordpress\wp content\themes\freshlife\template fullwidth.php:7)在C:\Users\aratnan\Desktop\wordpress\InstantWP_4.3\iwpserver\htdocs\wordpress\wp content\plugins\wp exec php\wp exec php.php(652):eval()'d code on line 4'中,您正在
模板fullwidth.php
中使用它,并且在运行代码之前主题已经输出了内容。@acr我更新了我的答案。这只是一个如何做的概念证明。您可能需要添加一个nonce或进行安全检查。@RRikesh:工作正常..谢谢。。用户是否可以在保存时选择文件名而不是test.txt?这段代码给了我错误`警告:无法修改标题信息-标题已由发送(输出开始于C:\Users\aratnan\Desktop\wordpress\InstantWP\u 4.3\iwpserver\htdocs\wordpress\wp content\themes\freshlife\template fullwidth.php:7)在C:\Users\aratnan\Desktop\wordpress\InstantWP_4.3\iwpserver\htdocs\wordpress\wp content\plugins\wp exec php\wp exec php.php(652):eval()'d code on line 4'中,您正在
模板fullwidth.php
中使用它,并且在运行代码之前主题已经输出了内容。@acr我更新了我的答案。这只是一个如何做的概念证明。您可能需要添加一个nonce或进行安全检查。@RRikesh:工作正常..谢谢。。用户是否可以在保存时选择文件名而不是test.txt?