irwan

PHP : Force file download

Mar 16th, 2012
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.74 KB | None | 0 0
  1. Some files, such as mp3, are generally played throught the client browser. If you prefer forcing download of such files, this is not a problem: The following code snippet will do that job properly.
  2.  
  3. <?php
  4. function downloadFile($file){
  5.         $file_name = $file;
  6.         $mime = 'application/force-download';
  7.     header('Pragma: public');   // required
  8.     header('Expires: 0');       // no cache
  9.     header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  10.     header('Cache-Control: private',false);
  11.     header('Content-Type: '.$mime);
  12.     header('Content-Disposition: attachment; filename="'.basename($file_name).'"');
  13.     header('Content-Transfer-Encoding: binary');
  14.     header('Connection: close');
  15.     readfile($file_name);       // push it out
  16.     exit();
  17. }
  18. ?>
Add Comment
Please, Sign In to add comment