11
08月
2015
13. 强制性文件下载
如果你需要下载特定的文件而不用另开新窗口,下面的代码片段可以帮助你。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
function force_download( $file )
{
$dir = "../log/exports/" ;
if ((isset( $file ))&&( file_exists ( $dir . $file ))) {
header( "Content-type: application/force-download" );
header( 'Content-Disposition: inline; filename="' . $dir . $file . '"' );
header( "Content-Transfer-Encoding: Binary" );
header( "Content-length: " . filesize ( $dir . $file ));
header( 'Content-Type: application/octet-stream' );
header( 'Content-Disposition: attachment; filename="' . $file . '"' );
readfile( "$dir$file" );
} else {
echo "No file selected" ;
}
}
|
语法:
1
2
3
|
<php
force_download( "image.jpg" );
?>
|
特殊说明,本文版权归 ning个人博客 所有带原创标签请勿转载,转载请注明出处.