Download text file on the fly

Download text file on the fly

by Abdul Bashet -
Number of replies: 0

Hi,

I want to make download a text file on the fly. I don't want to save the file any where in the server. I have some content in $my_file_data. I tried with the code below:

if (strpos($CFG->wwwroot, 'https://') === 0) {
        @header('Cache-Control: max-age=10');
        @header('Expires: '. gmdate('D, d M Y H:i:s', 0) .' GMT');
        @header('Pragma: ');
    } else { //normal http - prevent caching at all cost
        @header('Cache-Control: private, must-revalidate, pre-check=0, post-check=0, max-age=0');
        @header('Expires: '. gmdate('D, d M Y H:i:s', 0) .' GMT');
        @header('Pragma: no-cache');
    }
    header("Content-Type: application/download\n");
    header("Content-Disposition: attachment; filename="result.txt"");
    
    $content = $my_file_data;
    
    echo $content;

On out put I got this kind of error on dibug mode:

Warning: Cannot modify header information - headers already sent by (output started at /var/www/blah blah blah

and it's giving me echo output of $content.

What can I do to download of that content as text file on the fly?

Average of ratings: -