PHP: Stream Download

A general php code to download any type of file:

 
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.

Leave a Reply

Your email address will not be published. Required fields are marked *