php:将变量内容下载为文件

fac*_*cha 12 php file attachment download

主题可能吗?我有一个脚本正在执行.有一次,我在变量中有一大段文本.我是否可以将其作为可下载文件提供,而无需将可变内容写入磁盘?

<?php
    echo "Hello";
    //how do I make the content of this variable downloadable?
    $download_me = "download me...";
    echo "Bye";
?>
Run Code Online (Sandbox Code Playgroud)

d2b*_*rke 20

如果您的意思是让用户单击一个链接并弹出一个对话框,将某些内容保存为文本文件:

<?php
$download_me = "download me...";
header("Content-type: text/plain");
header("Content-Disposition: attachment; filename=test.txt");
echo $download_me;
?>
Run Code Online (Sandbox Code Playgroud)

那是你的目标吗?此外,如果设置了某个$_POST或某个$_GET变量,您可能还想编写一些只允许以这种方式发送标题的行.


Tha*_*ama 8

它应该如下所示:

<?php
  header("Content-type: text/plain");
  header("Content-Disposition: attachment; filename='whatever.txt'");
  echo $your_text;
?>
Run Code Online (Sandbox Code Playgroud)