Wednesday, December 3, 2008

Force Downloading a file

Forces a user to download a file, for e.g you have an image but you want the user to download it instead of displaying it in his browser.



header("Content-type: application/octet-stream");

// displays progress bar when downloading (credits to Felix ;-))
header("Content-Length: " . filesize('myImage.jpg'));

// file name of download file
header('Content-Disposition: attachment; filename="myImage.jpg"');

// reads the file on the server
readfile('myImage.jpg');

No comments: