473,789 Members | 2,447 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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">Downl oad 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&fil e=File.txt">Dow nload 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 8533
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">Downl oad 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&fil e=File.txt">Dow nload 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*******@attgl obal.net
=============== ===

Jan 1 '08 #2

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

Similar topics

2
2014
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 to start the download process: Response.Clear() Response.ContentType = "text/plain" Response.AppendHeader("Content-Disposition", "attachment; filename=" & fileName) Response.AppendHeader("Content-Description", "This is your Cost Journal...
4
3107
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 download the file on an as needed basis. DIM filename filename = "Companies.txt" Response.ContentType = "Unknown" Response.AddHeader "content-disposition", "attachment;filename=""" & filename & """"
0
1835
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 window. Well, thanks to Steve Orr, I have that working. However, I still have a few issues. First, the download dialog does not have the name of the file being downloaded in the "File name" field, instead it has the name of the aspx page that have...
7
3706
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 satisfactory answer (check your browser) so now that I've had the time to set up a reasonable little test that I can post somewhere, I'll try again. The app I've written has three ASPX pages. One is a combined page which writes a little text...
3
2768
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" The file being downloaded is a license file and MUST be stored with that ..lic extension. If I access this aspx page with IE, it prompts me if I want to open or save
2
4171
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 Application SDK 1.1. are installed on my local pc. My web server is Windows 2003 server Enterprise. Microsoft .NET Framework Version:1.1 and Microsoft Speech Server 2004 are installed on the server.
13
4968
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 please tell me how do i go about it?
3
1237
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
8343
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 Wang - September 27th 2006) My problem is to Print a text from a RichTextBox with all options like - AllowMultiplePages enabled plus PrintPreview and PageSetup. I tried 2 algorithms - 1 posted in Microsoft and 1 posted in this forum. ...
0
9499
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10177
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8995
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7519
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5404
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4076
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3677
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2898
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.