irwan

Download by using PHP

Mar 15th, 2012
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.91 KB | None | 0 0
  1.  
  2.  
  3. // local file that should be send to the client
  4. $local_file = 'test.zip';
  5. // filename that the user gets as default
  6. $download_file = 'your-download-name.zip';
  7.  
  8. if(file_exists($local_file) && is_file($local_file)) {
  9.     // send headers
  10.     header('Cache-control: private');
  11.     header('Content-Type: application/octet-stream');
  12.     header('Content-Length: '.filesize($local_file));
  13.     header('Content-Disposition: filename='.$download_file);
  14.  
  15.     // flush content
  16.     flush();
  17.  
  18.     /*
  19.     ** You can also remove the following part and
  20.     ** replace it trough a database command fetching
  21.     ** the download data
  22.     */
  23.     // open file stream
  24.     $file = fopen($local_file, "rb");
  25.  
  26.     // send the file to the browser
  27.     print fread ($file, filesize($local_file));
  28.  
  29.     // close file stream
  30.     fclose($file);}
  31. else {
  32.     die('Error: The file '.$local_file.' does not exist!');
  33. }
Add Comment
Please, Sign In to add comment