php: stream download

by prettyscripts on 2007-11-27 22:42:37 • Leave a comment »

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.

Your URL will be displayed.
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 />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)