如何在php中动态创建文件并下载

如何在php中动态创建文件并下载,php,download,Php,Download,我有下面的php脚本,它检查复选框元素的提交值,如果选中了,则打印它们的值 <?php echo '<table class="features-table">'; echo "<tbody>"; for ($i=1;$i<=2284;$i+=1) { if($_POST[$i]) { echo"<tr>"; echo "<td><a href=http://www.m-w.com/dicti

我有下面的php脚本,它检查复选框元素的提交值,如果选中了,则打印它们的值

<?php
echo '<table class="features-table">';

echo "<tbody>";
for ($i=1;$i<=2284;$i+=1) {
  if($_POST[$i]) {
    echo"<tr>";
            echo "<td><a href=http://www.m-w.com/dictionary/" . $_POST[$i] . ">" . $_POST[$i].  "</a></td>";
    echo "</tr>";
  }
}

?>
但我不知道如何将它与当前的php脚本集成


谢谢

我认为没有必要动态创建文件

我会用下面的方法做


使数据作为隐藏字段可用,并将字段与下载按钮一起包含在表单标记中。当用户单击下载按钮时,脚本中将提供所有表单数据。然后,您可以对数据执行任何您想执行的操作。

发布代码,这可能会在将来帮助其他人

在sudip给出答案后,我做了以下更改

<?php
$list = "Unknown words from GSL\n\n";
    echo '<table class="features-table">';
    echo "<tbody>";
    for ($i=1;$i<=2284;$i+=1) {
      if($_POST[$i]) {
        echo"<tr>";
                echo "<td><a href=http://www.m-w.com/dictionary/" . $_POST[$i] . ">" . $_POST[$i].  "</a></td>";
                $list = $list . $_POST[$i] . "\n";
        echo "</tr>";
      }
    }

echo'
   <form action="download.php" method="post">
   <input type="hidden" name="wordlist" value="'. $list . '">

   <center><input type="submit" name="submit_parse" style="margin-bottom: 20px; margin-top: 10px;width:200px; font-face: "Comic Sans MS"; font-size: larger; " value=" Download as a text file "> </center>
   </form>
';
?>

我必须创建单独的php文件,因为在发送任何实际输出之前必须调用header(),无论是通过普通HTML标记、文件中的空行还是从php

下载的内容

<?php
header("Content-type: text/plain");
header("Content-Disposition: attachment; filename=list.txt");
header("Pragma: no-cache");
header("Expires: 0");
echo $_POST["wordlist"] ;
?>


感谢您提出将内容存储在隐藏字段中的想法!因此,解决方案是您忘记在
download.php
的内容中添加
标记?
<?php
header("Content-type: text/plain");
header("Content-Disposition: attachment; filename=list.txt");
header("Pragma: no-cache");
header("Expires: 0");
echo $_POST["wordlist"] ;
?>