PHP文件向下带标题()

I have written the following code to allow download of files without the source being displayed.

In this example, the file 'test.pdf' is downloaded, but its corrupt.

Can anyone see any reason this would be the case?

$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;
}

Please use this method

$file receive the full path, $filename is the download filename.

public static function downloadFile($file, $filename)
    {
        if (file_exists($file)) {
            header('Content-Description: File Transfer');
            header('Content-Type: application/octet-stream');
            header('Content-Disposition: attachment; filename = '.basename($filename));
            header('Content-Transfer-Encoding: binary');
            header('Expires: 0');
            header('Cache-Control: must-revalidate, post-check = 0, pre-check = 0');
            header('Pragma: public');
            header('Content-Length: ' . filesize($file));
            ob_clean();
            flush();
            readfile($file);
            die;
        }
    }