downloading txt 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
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

downloading txt file

Post by itsmani1 »

I created a file on server now i want to download that file, how can i do so? i don't want to open that file in my web page i want to download this file

Please help

thanks
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

you need to use the header() function
There should be some useful functions on the user-contributed notes on this page:

Code: Select all

header()
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

specifically, take a look at 'content-disposition'.
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

Post by itsmani1 »

sorry.........

I don't want to download an already created file, I want to create file at runtime form databse and want to save it at local PC.


thanks much.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

you'll still use header() to do this.
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

Post by itsmani1 »

I tried this code but result is not what is required.

when i click on asdf it opens the file instead of downloading it.

Code: Select all

header('Content-Type: application/txt');
	header('Content-Disposition: attachment; filename=testFile.txt');
	echo "<a href=\"testFile.txt\">asdf</a>";
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

here is a sample header

Code: Select all

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public"); 
header("Content-Description: File Transfer");
header("Content-Type: application/text");
header("Content-Disposition: attachement; filename=testfile.txt");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize('testfile.txt'));
echo readfile('testfile.txt');
exit;
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

psst, we don't echo readfile() ;)
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

Post by itsmani1 »

i tried it at :

http://www.dizyn.com/bindex.php



and it showed me the text file contents. I want this to give me an option for saving file on my local disc.

Please help me i don't know what's the problem.

I tried by removing echo readfile('testfile.txt'); but it did not worked. I wanted to save file on my on PC not on server.

thanks for your help.
please help me thanks
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

I clicked your link and it asked if I wanted to save the file or open it :?
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

Not in IE... it just opens in the browser

It asks to save in firefox though.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

the only other thing I can think of then would be to change the content-type.

there are several mime types for text files.

here's a list:
http://filext.com/detaillist.php?extdet ... rch=Search
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

Post by itsmani1 »

I solved it this way:

Code: Select all

<?
	$myFile = "testFile.txt";
	$fh = fopen($myFile, 'w+') or die("can't open file");
	$stringData = "Hellozzz";
	fwrite($fh, $stringData);
	$fname = "testFile.txt";

	header("HTTP/1.1 200 OK");
	header("Content-Type: application/force-download");
	header("Content-Disposition: attachment; filename=$fname");
	header("Content-Transfer-Encoding: binary");
	echo readfile('testFile.txt');
?>
and now its working fine @ http://www.dizyn.com/bbindex.php
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

well done.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

don't echo readfile()... it is output automatically
Post Reply