473,386 Members | 1,908 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

text file download on .php page

Hello,
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
link
<a href="Download.php?dir=Dir&file=File.txt">Download file</a>
and prepare Download.php file:

<?php
if (isset($_GET['dir']) && isset($_GET['file']))
{
$file = $_GET['dir'] . '/' . $_GET['file'];
$fd = fopen($file, 'r');
$size = filesize($file);
$contents = fread($fd, $size);
fclose($fd);
header("Content-Type: application/octet-stream");
header("Content-Length: $size;");
header("Content-Disposition: attachement; filename=$file");
echo $contents;
}
?>

But... again the file contents is displayed instead of downloading.
Could you help me please to write it correctly?
Thank you!
/RAM/
Jan 1 '08 #1
1 8497
R.A.M. wrote:
Hello,
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
link
<a href="Download.php?dir=Dir&file=File.txt">Download file</a>
and prepare Download.php file:

<?php
if (isset($_GET['dir']) && isset($_GET['file']))
{
$file = $_GET['dir'] . '/' . $_GET['file'];
$fd = fopen($file, 'r');
$size = filesize($file);
$contents = fread($fd, $size);
fclose($fd);
header("Content-Type: application/octet-stream");
header("Content-Length: $size;");
header("Content-Disposition: attachement; filename=$file");
echo $contents;
}
?>

But... again the file contents is displayed instead of downloading.
Could you help me please to write it correctly?
Thank you!
/RAM/
Nothing is wrong with the code.

This is completely up to the browser. You can make recommendations, but
that's all.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Jan 1 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Tom Youngquist | last post by:
I am trying to download a text file that my .NET page has just created based on entered parameters on the web page. Everything seems to work and the file is created. I am using the following code...
4
by: Gary | last post by:
Hello, I am using asp classical and connection to SQL 2000 on a Windows 2000 machine. I am using this code to generate a text file when the user clicks on the link. This is so the user can...
0
by: Buddy Ackerman | last post by:
I am trying to implment a file download via a link such that when clicked, instead of starting the default application for that type of file the user will be presented with a download dialog...
7
by: theyas | last post by:
How can I get my code to NOT display two "Open/Save/Cancel/More Info" dialog boxes when using the "Response.WriteFile" method to download a file to IE I've asked about this before and didn't get a...
3
by: Nate Hekman | last post by:
I have an aspx page that generates a file on the fly for a person to download. It sends these http headers: Content-Type: text/plain Content-Disposition: attachment; filename="myfile.lic" ...
2
by: Onur | last post by:
Hi.All I'm working on a TTS application. It runs on my local pc (WindowXP pro) without any error. Microsoft Visual Studio .NET 2003, Microsoft .NET Framework SDK v1.1, Microsoft Speech...
13
by: sonald | last post by:
Hi, Can anybody tell me how to change the text delimiter in FastCSV Parser ? By default the text delimiter is double quotes(") I want to change it to anything else... say a pipe (|).. can anyone...
3
by: Samuel | last post by:
Hi I want to offer people to actually download text file and not view it on browser (which is what happens by default) Thank you, Samuel
2
by: deepakfordotnet | last post by:
Hi, First of all let me confess that I could not get the solution to the same problem from an earlier post Printing :by Mr.Richard MSL (dated September 24th 2006) working. (Replied by Mr.Walter...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.