Force downloading of a file


/ Published in: PHP
Save to your folder(s)

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.


Copy this code and paste it in your HTML
  1. header("Content-type: application/octet-stream");
  2.  
  3. // displays progress bar when downloading (credits to Felix ;-))
  4. header("Content-Length: " . filesize('myImage.jpg'));
  5.  
  6. // file name of download file
  7. header('Content-Disposition: attachment; filename="myImage.jpg"');
  8.  
  9. // reads the file on the server
  10. readfile('myImage.jpg');

URL: http://htmlblog.net/10-code-snippets-for-php-developers/

Report this snippet


Comments


You need to login to view comments.