PS:這段代碼在win系統下跑沒有問題,楓哥親測可用.如果用的是linux或是其他系統,要要諸位自行測試下啦.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
if (! is_file ( $filename )) { //檢測文件是否存在. die ( '下載的文件好像跟TM誰私奔啦!' ); } $filepath = str_replace ( '\\' , '/' , realpath ( $filename )); $filesize = filesize ( $filepath ); $filename = substr ( strrchr ( '/' . $filepath , '/' ), 1); $extension = strtolower ( substr ( strrchr ( $filepath , '.' ), 1)); // use this unless you want to find the mime type based on extension,文件后綴格式,不解釋. $mime = array ( 'application/octet-stream' ); header( 'Content-Type: ' . $mime ); header( 'Content-Disposition: attachment; filename="' . $filename . '"' ); header( 'Content-Transfer-Encoding: binary' ); header( 'Content-Length: ' .sprintf( '%d' , $filesize )); header( 'Expires: 0' ); // check for IE only headers,坑爹的IE檢測,你懂的. if (isset( $_SERVER [ 'HTTP_USER_AGENT' ]) &&(( strpos ( $_SERVER [ 'HTTP_USER_AGENT' ], 'MSIE' ) !== false))) { header( 'Cache-Control: must-revalidate, post-check=0, pre-check=0' ); header( 'Pragma: public' ); } else { header( 'Pragma: no-cache' ); } $handle = fopen ( $filepath , 'rb' ); fpassthru ( $handle ); fclose( $handle ); |
這個代碼是楓哥從國外的一個站點上找到的,親測可用.度娘出來的結果,你懂的.因此在博客上共享出來,希望對大家有用.國外的站,地址忘了,沒有辦法放出,理解下.最后,嚴肅的聲明下:這個代碼不是我寫的,是轉載的.