【发布时间】:2017-02-06 07:30:33
【问题描述】:
我编写了以下代码以允许在不显示源的情况下下载文件。
在本例中,文件“test.pdf”已下载,但已损坏。
谁能看出会出现这种情况的任何原因?
$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;
}
【问题讨论】:
标签: php header content-type mime