Page 1 of 1
downloading txt file
Posted: Mon Jul 17, 2006 1:27 pm
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
Posted: Mon Jul 17, 2006 1:29 pm
by Luke
you need to use the header() function
There should be some useful functions on the user-contributed notes on this page:
Posted: Mon Jul 17, 2006 1:32 pm
by Burrito
specifically, take a look at 'content-disposition'.
Posted: Mon Jul 17, 2006 1:39 pm
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.
Posted: Mon Jul 17, 2006 1:41 pm
by Burrito
you'll still use
header() to do this.
Posted: Mon Jul 17, 2006 1:57 pm
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>";
Posted: Mon Jul 17, 2006 2:04 pm
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;
Posted: Mon Jul 17, 2006 2:08 pm
by feyd
psst, we don't echo
readfile() 
Posted: Mon Jul 17, 2006 2:11 pm
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
Posted: Mon Jul 17, 2006 3:16 pm
by Burrito
I clicked your link and it asked if I wanted to save the file or open it

Posted: Mon Jul 17, 2006 3:20 pm
by Luke
Not in IE... it just opens in the browser
It asks to save in firefox though.
Posted: Mon Jul 17, 2006 3:22 pm
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
Posted: Tue Jul 18, 2006 1:10 pm
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
Posted: Tue Jul 18, 2006 1:13 pm
by Burrito
well done.
Posted: Tue Jul 18, 2006 1:13 pm
by Luke
don't echo readfile()... it is output automatically