0

我正在做一个项目,该项目的要求是在 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 文件。请帮我确定问题

4

3 回答 3

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>

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

于 2013-08-14T07:09:36.407 回答
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' );
于 2013-08-14T08:14:41.887 回答
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>
于 2013-08-14T06:54:51.437 回答