Place the file in a non-public folder and use readfile() to send the contents to the client, as well as header() to set the file's name, type, size, and so on.
Quick example, untested... to download
exam.pdf you'd load
files.php?id=1
PHP Code:
<?php # files.php
/* - - - - - - - - - -
* Our hidden files
*/
$files = array(
1 => 'exam.pdf',
2 => 'exam2.pdf',
3 => 'exam3.pdf',
);
/* - - - - - - - - - -
* Ensure `?id=xxx` is set and valid
*/
if ( ! isset( $_GET['id'] ) )
{
header( 'Location: /' );
exit;
}
$id = ( int ) $_GET['id'];
if ( ! isset( $files[ $id ] ) )
{
header( 'Location: /' );
exit;
}
/* - - - - - - - - - -
* Full path to the file
*/
$path = '/var/non-www/files/' . $files[ $id ];
/* - - - - - - - - - -
* Ensure $path is a valid file
*/
if ( ! is_file( $path ) )
{
header( 'Location: /' );
exit;
}
/* - - - - - - - - - -
* Send the file as a download with proper headers
*/
header( 'Content-type: application/octet-stream' );
header( 'Content-Disposition: attachment; filename="' . basename( $path ) . '"' );
header( 'Content-length: ' . filesize( $path ) );
readfile( $path );