Skip to content

Instantly share code, notes, and snippets.

@toddnestor
Last active January 16, 2017 01:08
Show Gist options
  • Save toddnestor/dd9c3c9a3cbbf221519c0838a97d4540 to your computer and use it in GitHub Desktop.
Save toddnestor/dd9c3c9a3cbbf221519c0838a97d4540 to your computer and use it in GitHub Desktop.
<?php
function forceDownload()
{
$file_name = "/some/directory/not/publicly/available/" . $_GET['file'];
$file = file_get_contents( $file_name );
if( !empty($file) )
{
header( 'Content-Description: File Transfer' );
header( 'Content-Type: application/octet-stream' );
header( 'Content-Disposition: attachment; filename=' . $_GET['file'] );
header( 'Content-Transfer-Encoding: binary' );
header( 'Connection: Keep-Alive' );
header( 'Expires: 0' );
header( 'Cache-Control: must-revalidate, post-check=0, pre-check=0' );
header( 'Pragma: public' );
header( 'Content-Length: ' . filesize( $file_name ) );
echo $file;
exit;
}
}
function authenticate()
{
//do authentication stuff and return true if authenticated, false otherwise
}
if( authenticate() )
{
forceDownload();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment