473,434 Members | 1,601 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,434 software developers and data experts.

Create a file (txt) to download

Hi guys, does anyone knows which are the functions I've to use to create
a file, in this case a txt or a csv, that will be asked in runtime to
the user to download it? So I suppose without creating any file on the
server. Something like the dump in phpmyadmin when we choose to save as.

Thx a lot, chr
--
__________________________________________________ _________________
{ Christian Giordano's site and blog @ http://cgws.nuthinking.com }
Jul 17 '05 #1
6 14301
Generate the following headers, then just output the file's contents.

header('Content-Type: application/octetstream; name="file.txt"');
header('Content-Type: application/octet-stream; name="file.txt"');
header('Content-Disposition: attachment; filename="file.txt"');

application/octetstream is for IE and Opera, application/octet-stream
is for the rest of browsers.

Jul 17 '05 #2
drdaeman wrote:
Generate the following headers, then just output the file's contents.

header('Content-Type: application/octetstream; name="file.txt"');
header('Content-Type: application/octet-stream; name="file.txt"');
header('Content-Disposition: attachment; filename="file.txt"');

application/octetstream is for IE and Opera, application/octet-stream
is for the rest of browsers.

Well, I see the procedure... but how if the file.txt doesn't exist? in
the sense, in this case is it presume that the file.txt exists on the
server or it is just the name of the file to save? in this last case,
how to write in?

Thanks a lot, chr

--
__________________________________________________ _________________
{ Christian Giordano's site and blog @ http://cgws.nuthinking.com }
Jul 17 '05 #3
drdaeman wrote:
Generate the following headers, then just output the file's contents.

header('Content-Type: application/octetstream; name="file.txt"');
header('Content-Type: application/octet-stream; name="file.txt"');
header('Content-Disposition: attachment; filename="file.txt"');


I tried putting just this lines in a php file and also if there si a
file "file.txt" in the same folder it makes me download the php file
(empty) itself :S

any ideas on what i'm making wrong?

thank u very much, chr
--
__________________________________________________ _________________
{ Christian Giordano's site and blog @ http://cgws.nuthinking.com }
Jul 17 '05 #4
Well... "file.txt" is just a filename provided to browser, that browser
will suggest when saving a file, nothing more.

If you want to make browser download some text file from your system
then use something like this:

<?php
$file = $_REQUEST['file'];
if (preg_match('^[A-Z0-9\-_\.]+$', $file)) {
if ($fp = @fopen('datafiles/' . $file . '.txt', 'rb')) {
header('Content-Type: application/octetstream; name="' . $file .
'.txt"');
header('Content-Type: application/octet-stream; name="' . $file .
'.txt"');
header('Content-Disposition: attachment; filename="' . $file .
'.txt"');
fpassthru($fp);
fclose($fp);
} else {
header('Status: 404 Not Found');
}
}
?>

If you want to generate the file contents on the fly without any
external .txt file, then:

<?php
header('Content-Type: application/octetstream; name="somename.txt"');
header('Content-Type: application/octet-stream;
name="somename.txt"');
header('Content-Disposition: attachment; filename="somename.txt"');
// Output file contents here
echo "This is a test. The browser should suggest to save it as
'somename.txt'";
?>

Jul 17 '05 #5
> <?php
header('Content-Type: application/octetstream; name="somename.txt"');
header('Content-Type: application/octet-stream;
name="somename.txt"');
header('Content-Disposition: attachment; filename="somename.txt"');
// Output file contents here
echo "This is a test. The browser should suggest to save it as
'somename.txt'";
?>


thanks a lot... next time it will be better if I copy and paste so I
won't change "n" with "u" :) my poor eyes... :)

c ya, chr

--
__________________________________________________ _________________
{ Christian Giordano's site and blog @ http://cgws.nuthinking.com }
Jul 17 '05 #6
drdaeman wrote:
Generate the following headers, then just output the file's
contents.

header('Content-Type: application/octetstream; name="file.txt"');
header('Content-Type: application/octet-stream; name="file.txt"');
header('Content-Disposition: attachment; filename="file.txt"');

application/octetstream is for IE and Opera,
application/octet-stream
is for the rest of browsers.


IE and Opera handle "application/octet-stream" very well, so there's
no need to have that useless first line in there. After all, there
should (and will) be only one Content-Type header being sent to the
browser anyway. Besides, when you're sending plaintext, the content
type should be "text/plain" - because you're already suggesting the
file to be downloaded as file instead of being displayed with the
Content-Disposition header... there's no valid reason to lie about the
content type :)

--
Markku Uttula

Jul 17 '05 #7

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

Similar topics

5
by: Brandon Walters | last post by:
I wrote a file download module for my website. The reason for the file download module is that my website downloads work on a credit based system. So I need to keep track of and limit daily...
9
by: Lauren Quantrell | last post by:
Is there a way to create a text file (such as a Windows Notepad file) by using a trigger on a table? What I want to do is to send a row of information to a table where the table: tblFileData has...
1
by: Lucas Tam | last post by:
You can try copying via Fileshare if you have access, or you can make IE pop up a download box? >-----Original Message----- >Good evening, > >Is there any way to do this? >I have a text file...
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...
4
by: Nathan Sokalski | last post by:
I want to give visitors to my site the option of downloading a generated ..txt file by clicking a button. I know how to generate text files, but how do I cause the browser to pop up one of those...
6
by: ABC | last post by:
I write the code as: <asp:LinkButton ID="LinkButton2" Text='<%# Eval("Title") %>' PostBackUrl='file://<%# Eval("path") %>' runat="server"></asp:LinkButton> But It don't work to download or open...
2
by: TOI DAY | last post by:
Hi all, How can I delete the file on the server after the user download it? For example: I have file name "123.txt" on a server, I copy it to "ABC.txt", then allow uer download the...
4
by: Vlad | last post by:
I am having problems using the file.create method within a function that is called when looping through an array of filepaths. If I call my function with a hardcoded file path --C:\Temp.txt the...
4
by: Roberto Mora | last post by:
I have not done programming in a very long time and what is worst, I never learned VB. Although my job does not require this knowledge, I cam across a problem that although it seemed simple it has...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...
0
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...
0
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...

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.