13

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";
?>
Kozuch
  • 2,272
  • 3
  • 26
  • 38
facha
  • 11,862
  • 14
  • 59
  • 82

2 Answers2

23

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.

Serrano
  • 1,457
  • 20
  • 17
d2burke
  • 4,081
  • 3
  • 39
  • 51
  • Thanks. But where do I stick my $download_me variable in? I don't have any files like test.txt stored on disk. – facha Sep 13 '10 at 12:50
  • 1
    you add the headers than echo. test.txt is the filename the user sees, not a file on the server – SamWM Sep 13 '10 at 12:53
  • Thanks for clarifying that for me Sam. @facha - make sure to accept and close the post out :) – d2burke Sep 13 '10 at 13:03
8

It should look like this:

<?php
  header("Content-type: text/plain");
  header("Content-Disposition: attachment; filename='whatever.txt'");
  echo $your_text;
?>
Thariama
  • 50,002
  • 13
  • 138
  • 166
  • So, is the answer that I have gone forever into the abyss? Is there any way to view my answer if I wanted to? – d2burke Aug 27 '12 at 19:55
  • @d2burke: Your answer still stands, but just the code block was improved (in fact, the code as you gave it doesn't work as it should because of the double quotes). You can click on the link "edited Aug 27 '12 at 15:36" below the answer to see your original answer and exactly what I changed. – Serrano Mar 22 '13 at 11:17
  • aaahh...i see. very cool. i've not seen that view before. Thanks Serrano – d2burke Mar 22 '13 at 12:41