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 相关文章推荐
phpfans留言版用到的install.php
Jan 04 PHP
PHP通过正则表达式下载图片到本地的实现代码
Sep 19 PHP
php中jQuery插件autocomplate的简单使用笔记
Jun 14 PHP
php常用ODBC函数集(详细)
Jun 24 PHP
php检测文件编码的方法示例
Apr 25 PHP
windows的文件系统机制引发的PHP路径爆破问题分析
Jul 28 PHP
PHP中怎样防止SQL注入分析
Oct 23 PHP
php根据日期或时间戳获取星座信息和生肖等信息
Oct 20 PHP
CodeIgniter扩展核心类实例详解
Jan 20 PHP
PHP与服务器文件系统的简单交互
Oct 21 PHP
php使用PDO获取结果集的方法
Feb 16 PHP
PHP常用操作类之通信数据封装类的实现
Jul 16 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初学者常见问题集合 修正版(21问答)
2010/03/23 PHP
php 中英文语言转换类代码
2011/08/11 PHP
PHP中foreach()用法汇总
2015/07/02 PHP
PHP+Ajax验证码验证用户登录
2016/07/20 PHP
php读取qqwry.dat ip地址定位文件的类实例代码
2016/11/15 PHP
php结合redis高并发下发帖、发微博的实现方法
2016/12/15 PHP
PHP操作MySQL中BLOB字段的方法示例【存储文本与图片】
2017/09/15 PHP
PHP如何防止XSS攻击与XSS攻击原理的讲解
2019/03/22 PHP
JavaScript和ActionScript的交互实现代码
2010/08/01 Javascript
JS二维数组的定义说明
2014/03/03 Javascript
node.js学习总结之调式代码的方法
2014/06/25 Javascript
jfreechart插件将数据展示成饼状图、柱状图和折线图
2015/04/13 Javascript
12种JavaScript常用的MVC框架比较分析
2015/11/16 Javascript
实例详解jQuery Mockjax 插件模拟 Ajax 请求
2016/01/12 Javascript
巧用canvas
2017/01/21 Javascript
Bootstrap学习笔记之进度条、媒体对象实例详解
2017/03/09 Javascript
详解微信小程序 相对定位和绝对定位
2017/05/11 Javascript
解决vue项目报错webpackJsonp is not defined问题
2018/03/14 Javascript
使用Vue.js和Flask来构建一个单页的App的示例
2018/03/21 Javascript
小程序如何定位所在城市及发起周边搜索
2020/02/11 Javascript
vue使用map代替Aarry数组循环遍历的方法
2020/04/30 Javascript
JS替换字符串中指定位置的字符(多种方法)
2020/05/28 Javascript
Python selenium如何设置等待时间
2016/09/15 Python
Windows下将Python文件打包成.EXE可执行文件的方法
2018/08/03 Python
15行Python代码实现网易云热门歌单实例教程
2019/03/10 Python
pycharm运行scrapy过程图解
2019/11/22 Python
numpy的Fancy Indexing和array比较详解
2020/06/11 Python
使用CSS3的appearance属性改变元素的外观的方法
2015/12/12 HTML / CSS
英国折扣高尔夫商店:Discount Golf Store
2019/11/19 全球购物
什么是动态端口(Dynamic Ports)?动态端口的范围是多少?
2014/12/12 面试题
销售员自我评价怎么写
2013/09/19 职场文书
内科护士实习自我鉴定
2013/10/17 职场文书
教师远程研修感悟
2015/11/18 职场文书
护士业务学习心得体会
2016/01/25 职场文书
Springboot使用Spring Data JPA实现数据库操作
2021/06/30 Java/Android
漫画《催眠麦克风-Dawn Of Divisions》第二卷PV公开
2022/04/05 日漫