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 相关文章推荐
Windows下PHP的任意文件执行漏洞
Oct 09 PHP
采用PHP函数memory_get_usage获取PHP内存清耗量的方法
Dec 06 PHP
关于PHP模板Smarty的初级使用方法以及心得分享
Jun 21 PHP
PHP错误提示的关闭方法详解
Jun 23 PHP
PHP入门之常量简介和系统常量
May 12 PHP
编写PHP脚本来实现WordPress中评论分页的功能
Dec 10 PHP
使用PHP如何实现高效安全的ftp服务器(一)
Dec 20 PHP
基于thinkPHP框架实现留言板的方法
Oct 17 PHP
PHP面向对象学习之parent::关键字
Jan 18 PHP
PHP对称加密算法(DES/AES)类的实现代码
Nov 14 PHP
php+lottery.js实现九宫格抽奖功能
Jul 21 PHP
Thinkphp5框架简单实现钩子(Hook)行为的方法示例
Sep 03 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 5.0创建图形的巧妙方法
2010/10/12 PHP
PHP实现根据银行卡号判断银行
2015/04/29 PHP
ThinkPHP实现简单登陆功能
2017/04/28 PHP
thinkphp5修改view到根目录实例方法
2019/07/02 PHP
基于Web标准的UI组件 — 树状菜单(2)
2006/09/18 Javascript
jquery.artwl.thickbox.js  一个非常简单好用的jQuery弹出层插件
2012/03/01 Javascript
JavaScript实现GriwView单列全选(自写代码)
2013/05/13 Javascript
JS实现鼠标经过好友列表中的好友头像时显示资料卡的效果
2014/07/02 Javascript
JavaScript中使用Object.prototype.toString判断是否为数组
2015/04/01 Javascript
JavaScript获取网页支持表单字符集的方法
2015/04/02 Javascript
网页中JS函数自动执行常用三种方法
2016/03/30 Javascript
jquery利用json实现页面之间传值的实例解析
2016/12/12 Javascript
微信小程序 video详解及简单实例
2017/01/16 Javascript
JS库之Particles.js中文开发手册及参数详解
2017/09/13 Javascript
Thinkjs3新手入门之如何使用静态资源目录
2017/12/06 Javascript
nodejs分离html文件里面的js和css的方法
2019/04/09 NodeJs
基于JS实现快速读取TXT文件
2020/08/25 Javascript
JS变量提升及函数提升实例解析
2020/09/03 Javascript
antd Form组件方法getFieldsValue获取自定义组件的值操作
2020/10/29 Javascript
[03:36]2014DOTA2 TI小组赛综述 八强诞生进军钥匙球馆
2014/07/15 DOTA
Python编程实现生成特定范围内不重复多个随机数的2种方法
2017/04/14 Python
Python实现的IP端口扫描工具类示例
2019/02/15 Python
python实现飞机大战游戏
2020/10/26 Python
python中for循环把字符串或者字典添加到列表的方法
2019/07/20 Python
Python3读取和写入excel表格数据的示例代码
2020/06/09 Python
canvas 实现 github404动态效果的示例代码
2017/11/15 HTML / CSS
英国手机零售商:Carphone Warehouse
2018/06/06 全球购物
大学生预备党员自我评价分享
2013/11/16 职场文书
2014学习优秀共产党员先进事迹材料思想汇报
2014/09/14 职场文书
简单租房协议书
2014/10/21 职场文书
幼儿园家长反馈意见
2015/06/03 职场文书
辩论赛新闻稿
2015/07/17 职场文书
《圆明园的毁灭》教学反思
2016/02/16 职场文书
咖啡厅里的创业计划书
2019/08/21 职场文书
游戏开发中如何使用CocosCreator进行音效处理
2021/04/14 Javascript
最新动漫情报:2022年7月新番定档超过30部, OVERLORD骨王第四季也在其中噢
2022/05/04 日漫