有些文件需要经过身份验证以后才能下载,我们不容用户知道下载的地址,甚至文件不存放在web文件夹下,感觉是不是做起来比较难呢?用PHP几行就可以了。这是在PHP官方手册提供的例子。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php $file = 'monkey.gif'; if (file_exists($file)) { header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename='.basename($file)); header('Content-Transfer-Encoding: binary'); header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Pragma: public'); header('Content-Length: ' . filesize($file)); ob_clean(); flush(); readfile($file); } ?> |