php: stream download

by prettyscripts on 2007-11-27 22:42:37

php

a general php code to download any type of file:

PHP:

header("Content-Type: application/octet-stream"); 
header("Content-Length: " filesize($filename)); 
header("Content-Disposition: attachment; filename=\"$filename\""); 
readfile($filename);

content-type defines the type of file being sent. application/octet-stream is used for any kind of files.

content-length indicates the size of information. this is to let browser know how much data to download in advance and ensure

content-disposition is used to suggest the file name to be saved. the filename should have the correct extension so that the browser knows the default application associated with this extension.

Tags: download, header, php

No feedback yet

Leave a comment


Your email address will not be revealed on this site.
PoorExcellent
note: all comments are moderated. do not spam and do not advertise. only comments relevant to the post will be published.
(Line breaks become <br />)
(For my next comment on this site)
(Allow users to contact me through a message form -- Your email will not be revealed!)