[SOLVED] - Force Download a text file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

[SOLVED] - Force Download a text file

Post 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
Last edited by anjanesh on Sat Mar 19, 2005 12:52 am, edited 1 time in total.
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post 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.
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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);

?>
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post 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.
Last edited by anjanesh on Fri Mar 18, 2005 10:46 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you are overwriting isbn.txt with php code that will send it's own source code to the user.
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post 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.
Post Reply