网站首页 文章专栏 如何做到点击图片的链接不是打开而是下载呢?
如何做到点击图片的链接不是打开而是下载呢?
a标签有个download属性
1 |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
/** * 强制下载文件
* @param string $file 文件路径
*/
function force_download( $file )
{ if ((isset( $file )) && ( file_exists ( $file ))) {
header( "Content-length: " . filesize ( $file ));
header( 'Content-Type: application/octet-stream' );
header( 'Content-Disposition: attachment; filename="' . basename ( $file ). '"' );
readfile( $file );
} else {
echo "No file selected" ;
}
} //使用示例 force_download( './test.jpg' );
|
ok,搞定!
转载请注明出处!