php利用header函数下载各种文件


Posted in PHP onAugust 24, 2016

本文实例为大家分享了php header函数下载文件实现代码,供大家参考,具体内容如下

http://www.php.net/manual/en/function.readfile.php

<?php
/**
* 下载文件
* header函数
*
*/


dl_file($_GET ['filename']);

function dl_file($file)
{
 $file = ".//images//" . $file;
 //First, see if the file exists
 
 if (! is_file ( $file ))
 {
  die ( "<b>404 File not found!</b>" );
 }
 
 // Gather relevent info about file
 $len = filesize ( $file );
 $filename = basename ( $file );
 $file_extension = strtolower ( substr ( strrchr ( $filename, "." ), 1 ) );
 
 // This will set the Content-Type to the appropriate setting for the file
 switch ($file_extension)
 {
  case "pdf" :
   $ctype = "application/pdf";
   break;
  case "exe" :
   $ctype = "application/octet-stream";
   break;
  case "zip" :
   $ctype = "application/zip";
   break;
  case "doc" :
   $ctype = "application/msword";
   break;
  case "xls" :
   $ctype = "application/vnd.ms-excel";
   break;
  case "ppt" :
   $ctype = "application/vnd.ms-powerpoint";
   break;
  case "gif" :
   $ctype = "image/gif";
   break;
  case "png" :
   $ctype = "image/png";
   break;
  case "jpeg" :
  case "jpg" :
   $ctype = "image/jpg";
   break;
  case "mp3" :
   $ctype = "audio/mpeg";
   break;
  case "wav" :
   $ctype = "audio/x-wav";
   break;
  case "mpeg" :
  case "mpg" :
  case "mpe" :
   $ctype = "video/mpeg";
   break;
  case "mov" :
   $ctype = "video/quicktime";
   break;
  case "avi" :
   $ctype = "video/x-msvideo";
   break;
  
  // The following are for extensions that shouldn't be downloaded
  // (sensitive stuff, like php files)
  case "php" :
  case "htm" :
  case "html" :
  case "txt" :
   die ( "<b>Cannot be used for " . $file_extension . " files!</b>" );
   break;
  
  default :
   $ctype = "application/force-download";
 }
 
 
 $file_temp = fopen ( $file, "r" );
 
 
 // Begin writing headers
 header ( "Pragma: public" );
 header ( "Expires: 0" );
 header ( "Cache-Control: must-revalidate, post-check=0, pre-check=0" );
 header ( "Cache-Control: public" );
 header ( "Content-Description: File Transfer" );
 // Use the switch-generated Content-Type
 header ( "Content-Type: $ctype" );
 // Force the download
 $header = "Content-Disposition: attachment; filename=" . $filename . ";";
 header ( $header );
 header ( "Content-Transfer-Encoding: binary" );
 header ( "Content-Length: " . $len );
 
 
 //@readfile ( $file );
 echo fread ( $file_temp, filesize ( $file ) );
 fclose ( $file_temp );
 
 exit ();
}

?>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

PHP 相关文章推荐
把PHP安装为Apache DSO
Oct 09 PHP
PHP Session_Regenerate_ID函数双释放内存破坏漏洞
Jan 27 PHP
PHPUnit PHP测试框架安装方法
Mar 23 PHP
php不写闭合标签的好处
Mar 04 PHP
PHP+javascript制作带提示的验证码源码分享
May 28 PHP
用PHP代替JS玩转DOM的思路及示例代码
Jun 15 PHP
php利用cookies实现购物车的方法
Dec 10 PHP
php自动更新版权信息显示的方法
Jun 19 PHP
PHP正则替换函数preg_replace()报错:Notice Use of undefined constant的解决方法分析
Feb 04 PHP
php使用GD2绘制几何图形示例
Feb 15 PHP
利用PHP扩展Xhprof分析项目性能实践教程
Sep 05 PHP
laravel框架中路由设置,路由参数和路由命名实例分析
Nov 23 PHP
php强制下载文件函数
Aug 24 #PHP
PHP简单实现数字分页功能示例
Aug 24 #PHP
PHP自定义函数获取URL中一级域名的方法
Aug 23 #PHP
PHP简单获取网站百度搜索和搜狗搜索收录量的方法
Aug 23 #PHP
PHP简单判断手机设备的方法
Aug 23 #PHP
PHP实现批量检测网站是否能够正常打开的方法
Aug 23 #PHP
PHP Cookie学习笔记
Aug 23 #PHP
You might like
PHP编程实现计算抽奖概率算法完整实例
2017/08/09 PHP
PHP递归的三种常用方式
2019/02/28 PHP
excel操作之Add Data to a Spreadsheet Cell
2007/06/12 Javascript
JavaScript入门之基本函数详解
2011/10/21 Javascript
JS限制上传图片大小不使用控件在本地实现
2012/12/19 Javascript
jQuery弹出框代码封装DialogHelper
2015/01/30 Javascript
分享一则JavaScript滚动条插件源码
2015/03/03 Javascript
js密码强度校验
2015/11/10 Javascript
jQuery的ready方法实现原理分析
2016/10/26 Javascript
Vue.2.0.5过渡效果使用技巧
2017/03/16 Javascript
webpack独立打包和缓存处理详解
2017/04/03 Javascript
详解express与koa中间件模式对比
2017/08/07 Javascript
浅谈vue中使用图片懒加载vue-lazyload插件详细指南
2017/10/23 Javascript
create-react-app修改为多页面支持的方法
2018/05/17 Javascript
vue中slot(插槽)的介绍与使用
2018/11/12 Javascript
微信小程序实现单选选项卡切换效果
2020/06/19 Javascript
微信小程序点击保存图片到本机功能
2019/12/13 Javascript
javascript实现留言板功能
2020/02/08 Javascript
[05:22]DOTA2 2015国际邀请赛中国区预选赛首日TOP10
2015/05/26 DOTA
python中的内置函数getattr()介绍及示例
2014/07/20 Python
Python中无限元素列表的实现方法
2014/08/18 Python
pip指定python位置安装软件包的方法
2019/07/12 Python
PyCharm 在Windows的有用快捷键详解
2020/04/07 Python
TensorFlow-gpu和opencv安装详细教程
2020/06/30 Python
零基础学Python之前需要学c语言吗
2020/07/21 Python
西尔斯百货官网:Sears
2016/09/06 全球购物
卡骆驰英国官网:Crocs英国
2019/08/22 全球购物
英国家居用品和床上用品零售商:P&B Home
2020/01/16 全球购物
short s1 = 1; s1 = s1 + 1;有什么错? short s1 = 1; s1 += 1;有什么错?
2014/09/26 面试题
大学生自我鉴定
2013/12/08 职场文书
公司业务员岗位职责
2014/03/18 职场文书
政治学求职信
2014/06/03 职场文书
代领学位证书毕业证书委托书
2014/09/30 职场文书
贷款承诺书
2015/01/20 职场文书
2015年班组工作总结
2015/04/20 职场文书
新入职员工工作总结
2015/10/15 职场文书