有时我们不想让浏览器直接打开文件,如PDF文件,而是要直接下载文件,那么以下函数可以强制下载文件,函数中使用了application/octet-stream头类型。
1 function download( $filename ){ 2 3 if (( isset ( $filename ))&&( file_exists ( $filename ))){ 4 5 header ("Content-length: ". filesize ( $filename )); 6 7 header ('Content-Type: application/octet-stream' ); 8 9 header ('Content-Disposition: attachment; filename="' . $filename . '"' ); 10 11 readfile (" $filename " ); 12 13 } else { 14 15 echo "Looks like file does not exist!" ; 16 17 } 18 19 } 20 21 // 使用方法如下 22 23 download('/down/test_45f73e852.zip');
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did29795