php: download variable content as file

人走茶凉 提交于 2019-12-20 13:04:44

问题


Is the subject possible? I have a script executing. At one point I have a large piece of text in a variable. Could I make it available as a downloadable file without actually writing variable content to disk?

<?php
    echo "Hello";
    //how do I make the content of this variable downloadable?
    $download_me = "download me...";
    echo "Bye";
?>

回答1:


If you mean letting the user click a link and have a dialog box pop up to save certain content as a text file:

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

Is that what you're aiming at? Also, you might want to write a few lines that only allows the headers to be sent this way if a certain $_POST or $_GET variable is set.




回答2:


It should look like this:

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


来源:https://stackoverflow.com/questions/3700471/php-download-variable-content-as-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!