[SOLVED] - Force Download a text file
Moderator: General Moderators
[SOLVED] - Force Download a text file
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
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
Last edited by anjanesh on Sat Mar 19, 2005 12:52 am, edited 1 time in total.
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
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);
?>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.
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.
Last edited by anjanesh on Fri Mar 18, 2005 10:46 pm, edited 1 time in total.
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.
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.
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
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.
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.