将html textarea内容下载为文件

时间:2013-08-14 06:40:52

标签: 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">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文件。请帮我识别问题

3 个答案:

答案 0 :(得分:3)

在代码中修改了一点。

首先把你的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>

在提交按钮中添加名称属性。

答案 1 :(得分:1)

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

<?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>

首先,您应该在屏幕上打印任何内容之前输出文件;其次,您忘记将name="submit"添加到原始代码中。我没有在这里使用它,但我收录了你的想法。

<小时/> 在WordPress上下文中: 将其添加到您的functions.php或插件文件中:

function feed_text_download(){
if(isset($_POST['text_to_download']))
 {
   header('Content-disposition: attachment; filename=test.txt');
   header('Content-type: application/txt');
   echo $_POST['text_to_download'];
   exit; //stop writing
 }
}
add_action('after_setup_theme', 'feed_text_download');

将表单添加到您的某个模板文件或帖子的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>

应该不错:)

<小时/> 编辑:添加文件名

HTML:

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

PHP:

function feed_text_download() {
  if ( isset( $_POST['text_to_download'] ) && isset( $_POST['filename'] ) ) {

    $filename = sanitize_file_name( $_POST['filename'] );

    header( 'Content-disposition: attachment; filename=' . $filename );
    header( 'Content-type: application/txt' );
    echo $_POST['text_to_download'];
    exit; //stop writing
  }
}

add_action( 'after_setup_theme', 'feed_text_download' );

答案 2 :(得分:0)

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

<?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>