Skip to content

Instantly share code, notes, and snippets.

@niksudan
Last active August 29, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niksudan/9e049c102786f0b7304d to your computer and use it in GitHub Desktop.
Save niksudan/9e049c102786f0b7304d to your computer and use it in GitHub Desktop.
Download a file [PHP]
<?php
/**
* Download a file
*
* @param string $file
* @return void
*/
function download_file( $file )
{
if ( file_exists($file) && $file != $_SERVER['DOCUMENT_ROOT'] ) {
header('Content-Description: File Transfer' );
header('Content-Type: application/octet-stream' );
header( 'Content-Disposition: attachment; filename=' . basename( $file ) );
header( 'Expires: 0' );
header( 'Cache-Control: must-revalidate' );
header( 'Pragma: public' );
header( 'Content-Length: ' . filesize( $file ) );
ob_clean();
flush();
readfile( $file );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment