469,306 Members | 1,987 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,306 developers. It's quick & easy.

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 13867
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Lauren Quantrell | last post: by
1 post views Thread by Lucas Tam | last post: by
4 posts views Thread by Nathan Sokalski | last post: by
6 posts views Thread by ABC | last post: by
2 posts views Thread by TOI DAY | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.