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删除页面记录 同时刷新页面 删除条件用GET方式获得
Jan 10 PHP
php float不四舍五入截取浮点型字符串方法总结
Oct 28 PHP
PHP 导出Excel示例分享
Aug 18 PHP
帝国cms目录结构分享
Jul 06 PHP
浅谈php错误提示及查错方法
Jul 14 PHP
Smarty高级应用之缓存操作技巧分析
May 14 PHP
PHP preg_match实现正则表达式匹配功能【输出是否匹配及匹配值】
Jul 19 PHP
php strftime函数的详细用法
Jun 21 PHP
Laravel-admin之修改操作日志的方法
Sep 30 PHP
在phpstudy集成环境下的nginx服务器下配置url重写
Dec 02 PHP
php 中的信号处理操作实例详解
Mar 04 PHP
php实现根据身份证获取精准年龄
Feb 26 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 setcookie(name, value, expires, path, domain, secure) 参数详解
2013/06/28 PHP
PHP 图片合成、仿微信群头像的方法示例
2019/10/25 PHP
Mootools 1.2教程 Tooltips
2009/09/15 Javascript
JS 表单验证大全
2011/11/23 Javascript
JS求平均值的小例子
2013/11/29 Javascript
indexOf 和 lastIndexOf 使用示例介绍
2014/09/02 Javascript
浅谈javascript 归并方法
2015/01/21 Javascript
Javascript实现div层渐隐效果的方法
2015/05/30 Javascript
原生js实现移动端瀑布流式代码示例
2015/12/18 Javascript
Angular的事件和表单详解
2016/12/26 Javascript
bootstrap table 表格中增加下拉菜单末行出现滚动条的快速解决方法
2017/01/05 Javascript
深入理解Vue2.x的虚拟DOM diff原理
2017/09/27 Javascript
前端axios下载excel文件(二进制)的处理方法
2018/07/31 Javascript
Vue项目实现换肤功能的一种方案分析
2019/08/28 Javascript
vue+vant-UI框架实现购物车的复选框全选和反选功能
2019/11/05 Javascript
JS+html5实现异步上传图片显示上传文件进度条功能示例
2019/11/09 Javascript
js判断在哪个浏览器打开项目的方法
2020/01/21 Javascript
JavaScript对象原型链原理详解
2020/02/05 Javascript
在 Vue 中使用 JSX 及使用它的原因浅析
2020/02/10 Javascript
vue实现公告栏文字上下滚动效果的示例代码
2020/06/16 Javascript
python基础教程之获取本机ip数据包示例
2014/02/10 Python
python获取mp3文件信息的方法
2015/06/15 Python
python+VTK环境搭建及第一个简单程序代码
2017/12/13 Python
Python Flask前后端Ajax交互的方法示例
2018/07/31 Python
解决PyCharm的Python.exe已经停止工作的问题
2018/11/29 Python
TensorFlow梯度求解tf.gradients实例
2020/02/04 Python
详解Scrapy Redis入门实战
2020/11/18 Python
什么是ESB?请介绍一下ESB?
2015/05/27 面试题
工商管理专业实习大学生自我鉴定
2013/09/19 职场文书
房地产还款计划书
2014/01/10 职场文书
建议书怎么写
2014/03/12 职场文书
中餐厅经理岗位职责
2014/04/11 职场文书
公司租车协议书
2015/01/29 职场文书
2015年幼儿园个人工作总结
2015/04/25 职场文书
幼儿园托班开学寄语(2016秋季)
2015/12/03 职场文书
springboot拦截器无法注入redisTemplate的解决方法
2021/06/27 Java/Android