Page 1 of 1

[SOLVED] - Force Download a text file

Posted: Fri Mar 18, 2005 11:19 am
by anjanesh
I have given a link : <a href="somfile.txt">Download File</a>
Instead the browser shows it in its browser window.
What is the method to force download - get the browser show you the dialog box for downloading the file. exe, zip etc will automatically be shown for downloading - but is there any way to force the browser to show the download dialog to download the txt file ?
Thanks

Posted: Fri Mar 18, 2005 11:25 am
by neophyte
I believe that is all controlled by the browser and what file types the browser recognizes. If I were you I would zip the txt file up. Most browsers recognize it as a file for downloading. I haven't run into one that didn't yet.

Posted: Fri Mar 18, 2005 3:16 pm
by m3mn0n

Posted: Fri Mar 18, 2005 4:17 pm
by Chris Corbyn
It's in the manual, I'm guessing that the easiest way to force a download of .txt is to trick the browser into believing it's an unknown file type.

Code: Select all

<?php

//File details
$file = './your_dir/yourfile.txt';
$type = 'application/octet-stream';
$length = filesize($file);
$name = 'yourfile.txt';

//Send to browser
header('Content-type: '.$type);
header('content-length:'. $length);
header('Content-disposition: attachment; filename='.$name);
@readfile($file);

?>

Posted: Fri Mar 18, 2005 10:01 pm
by anjanesh
Its showing the php source as well - http://vlbjcas.com/test/isbn.txt
BTW, this file is generated each time a particular script is called.

Code: Select all

$handle = fopen("isbn.txt","w");
 	fwrite($handle,"
         <?php
         header('Content-type: application/octet-stream');
         header('Content-disposition: attachment; filename=isbn.txt');
         @readfile('isbn.txt');
         ?>
         ");
// Write other text content into the text file.

Posted: Fri Mar 18, 2005 10:38 pm
by feyd
you are overwriting isbn.txt with php code that will send it's own source code to the user.

Posted: Fri Mar 18, 2005 10:52 pm
by anjanesh
I made a php script that displays a huge html table - dynamic - according to $_GET values.
Right above the table I've given a link : Download File which is actually a txt file separated by \t and \n.
This txt file is created new each time the script is called to make this huge table so that the user can view the text version instead.
Each time I ouput a row to html I write the same row to the text file.

Posted: Fri Mar 18, 2005 11:06 pm
by feyd
writing php code into a text file will not make the text file magically be php code. Furthermore, if run, the file passed to the user will contain the entire source to the file, plus the tab delimted data again.

Simply output the text data to the file. Run the php code you so desperately seem to want to use for it then. This code should be what's linked to, not the text file itself, if you want it downloaded.

Posted: Sat Mar 19, 2005 12:43 am
by anjanesh
I just changed isbn.txt to isbn.txt.php.
Solved - Got it to read.
Had to remove @readfile('isbn.txt.php'); though and unwanted tabs before php code - which feyd mentioned.