Do you have a question? Post it now! No Registration Necessary. Now with pictures!
- Posted on
- text file download on .php page
January 1, 2008, 1:07 pm
rate this thread
I am learning PHP5. I have little experience.
I have created a website in which I want to put a link to download a text
file. When I used simple
<a href="Dir/File.txt">Download file</a>
I had such problem that instead of dowloading the file contents was
displayed in browser. In some book on PHP5 I found a "solution": create a
<a href="Download.php?dir=Dir&file=File.txt">Download file</a>
and prepare Download.php file:
if (isset($_GET['dir']) && isset($_GET['file']))
$file = $_GET['dir'] . '/' . $_GET['file'];
$fd = fopen($file, 'r');
$size = filesize($file);
$contents = fread($fd, $size);
header("Content-Disposition: attachement; filename=$file");
But... again the file contents is displayed instead of downloading.
Could you help me please to write it correctly?
Re: text file download on .php page
Nothing is wrong with the code.
This is completely up to the browser. You can make recommendations, but
Remove the "x" from my email address
JDS Computer Training Corp.