How can I send a file using php, for example say how images get sent by something like:Is there a way to send a header telling the browser its about to recieve a file for downloading.PHP Code:header("Content-type: image/png");
.....
Thanx
Printable View
How can I send a file using php, for example say how images get sent by something like:Is there a way to send a header telling the browser its about to recieve a file for downloading.PHP Code:header("Content-type: image/png");
.....
Thanx
I just tried this:
And it popped up the download dialog. Is that what you want?Code:header('Content-type: image/gif');
header('Content-Disposition: attachment; filename="image.gif"');
readfile('image.gif');
Kind of but I'm more after being able to get it to download Zip files.
For a zip file, just do:
And it will prompt to download the zip file.
Thanx, I'll give it a go. :D
Humm, this was strange, I just made that change and when I requested a file it displayed the file in text as garbage. However just as I was about to post a reply I tired it one last time and it worked. I hadn't even edited the file :confused:. Any how it works now so thank you very much.
if you are going to send a file in the header then you should be using these as well,
header("Expires: Mon, 26 Nov 1962 00:00:00 GMT");
header("Last-Modified: " . gmdate("D,d M Y H:i:s") . " GMT");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
header("Content-type: application/x-zip");
header("Content-disposition: inline; filename=zip.zip);
header('Location: http://www.site.com/location/of/zip.zip');
Why?
because then this doesn't happen. you have to get the browser prepared to send it like that. it isn't like a link.
Quote:
Originally posted by Electroman
Humm, this was strange, I just made that change and when I requested a file it displayed the file in text as garbage.
I've never had any problem just doing header('Location: url'); before. If you could point me into the direction of some documentation, that'd be great.
I am surprised you haven't as there are way to many bugs in IE and other browsers to get it right.
http://us2.php.net/manual/en/function.header.php
read through the comments and you will see some of the bugs and why those headers are important.
sending the zip file in the header and having php render the page as text/html and not the application/zip is what causes most of the problems.
the "Expires and Last-Modified" may not be as important but I just had them in my notes so I posted them as well.
Reading that page would it not be better using:
readfile('http://www.site.com/location/of/zip.zip')
instead of:
header('Location: http://www.site.com/location/of/zip.zip')
It mentions problem with old browsers tho, any idea what happens when these problems strike?
you can use readfile if you are doing it that way. and what happens? well you experienced it yourslef, it loads the text in the browser. but that is only 1 of many bugs. I can't remeber them all. :)
For general binary data, use a application/octet-stream MIME type (Cotent-type). For others, you should look up the MIME type. Don't know where to best look for it though.
That's all jabberwocky to me.
Then you'd best read up on the HTTP protocol...
No. I refuse to learn.Quote:
Originally posted by CornedBee
Then you'd best read up on the HTTP protocol...