PHP File Down with header()
<p>I have written the following code to allow download of files without the source being displayed.</p>

<p>In this example, the file 'test.pdf' is downloaded, but its corrupt.</p>

<p>Can anyone see any reason this would be the case?</p>

<pre><code>$path = 'http://www.domain.org/images/uploads/test.pdf';

// Directory Path
$directory_path_filename = str_replace('http://www.domain.org/', '', $path);

$filepath = '/var/sites/l/domain.org/public_html/'.$directory_path_filename;

if ( file_exists( $filepath ) ) {

    $finfo = finfo_open( FILEINFO_MIME );
    header( 'Content-Type: application/pdf' );
    header( 'Content-Disposition: attachment; filename= ' . basename( $filepath ) );
    header( 'Content-Length: ' . filesize( $filepath ) );
    header( 'Expires: 0' );
    finfo_close( $finfo );

    ob_clean( );
    flush( );
    readfile( $filepath );
    exit;
}
</code></pre>