强制下载通常默认不会下载的文件
2006-05-03  作者:amao  同分类文章
description:
如图片和html网页文件。用法很简单,后面两个参数没有必要设定,前两个是文件和文件名。
  1. <?php
  2. function force_download ($data, $name, $mimetype='', $filesize=false) {
  3.     // File size not set?
  4.     if ($filesize == false OR !is_numeric($filesize)) {
  5.         $filesize = strlen($data);
  6.     }
  7.  
  8.     // Mimetype not set?
  9.     if (empty($mimetype)) {
  10.         $mimetype = 'application/octet-stream';
  11.     }
  12.  
  13.     // Make sure there's not anything else left
  14.     ob_clean_all();
  15.  
  16.     // Start sending headers
  17.     header("Pragma: public"); // required
  18.     header("Expires: 0");
  19.     header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  20.     header("Cache-Control: private",false); // required for certain browsers
  21.     header("Content-Transfer-Encoding: binary");
  22.     header("Content-Type: " . $mimetype);
  23.     header("Content-Length: " . $filesize);
  24.     header("Content-Disposition: attachment; filename="" . $name . "";" );
  25.  
  26.     // Send data
  27.     echo $data;
  28.     die();
  29. }
  30.  
  31. function ob_clean_all () {
  32.     $ob_active = ob_get_length () !== false;
  33.     while($ob_active) {
  34.         ob_end_clean();
  35.         $ob_active = ob_get_length () !== false;
  36.     }
  37.  
  38.     return true;
  39. }
  40. ?>


相关
《极品飞车11》发布在即 预告视频欣赏
the time of our lives (SONG OF FIFA WORLD CUP 2006)
获取MySQL数据表的大小
如何看待开源软件的知识产权问题
民进党2008大选候选人谢长廷简历(履历)
美国费城将提供城市无线宽带接入服务
简历撰写技巧
利用Nginx的X-Accel-Redirect头实现下载控制
Deepfish??手机浏览器新贵 下载地址
昆明至北京西K472/471次火车时刻表