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脚本的10个技巧(1)
Oct 09 PHP
PHP中for循环语句的几种变型
Nov 26 PHP
php一次性删除前台checkbox多选内容的方法
Sep 22 PHP
php实现json编码的方法
Jul 30 PHP
是 WordPress 让 PHP 更流行了 而不是框架
Feb 03 PHP
详解WordPress中的头像缓存和代理中的缓存更新方法
Mar 01 PHP
PHP分页初探 一个最简单的PHP分页代码的简单实现
Jun 21 PHP
Yii2下点击验证码的切换实例代码
Mar 14 PHP
Yii2压缩PHP中模板代码的输出问题
Aug 28 PHP
PHP基于session.upload_progress 实现文件上传进度显示功能详解
Aug 09 PHP
Yii 框架使用数据库(databases)的方法示例
May 19 PHP
PHP copy函数使用案例代码解析
Sep 01 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(视频)Http下载
2006/12/12 PHP
php中的登陆login
2007/01/18 PHP
jquery JSON的解析方式
2009/07/25 Javascript
浅谈JavaScript字符串拼接
2015/06/25 Javascript
js仿淘宝和百度文库的评分功能
2016/05/15 Javascript
JS跨域交互(jQuery+php)之jsonp使用心得
2016/07/01 Javascript
JS在Chrome浏览器中showModalDialog函数返回值为undefined的解决方法
2016/08/03 Javascript
详解js中常规日期格式处理、月历渲染和倒计时函数
2016/12/28 Javascript
详解JS数组Reduce()方法详解及高级技巧
2017/08/18 Javascript
JavaScript重复元素处理方法分析【统计个数、计算、去重复等】
2017/12/14 Javascript
详解Immutable及 React 中实践
2018/03/01 Javascript
利用vue和element-ui设置表格内容分页的实例
2018/03/02 Javascript
JS实现指定区域的全屏显示功能示例
2019/04/25 Javascript
Vee-validate 父组件获取子组件表单校验结果的实例代码
2019/05/20 Javascript
详解ES6 Promise的生命周期和创建
2019/08/18 Javascript
node.js使用yargs处理命令行参数操作示例
2020/02/11 Javascript
[03:14]2014DOTA2西雅图国际邀请赛 EG战队巡礼
2014/07/07 DOTA
Python中subprocess模块用法实例详解
2015/05/20 Python
简单谈谈Python中的反转字符串问题
2016/10/24 Python
人工智能最火编程语言 Python大战Java!
2017/11/13 Python
Python获取昨天、今天、明天开始、结束时间戳的方法
2018/06/01 Python
python2.x实现人民币转大写人民币
2018/06/20 Python
python通过txt文件批量安装依赖包的实现步骤
2019/08/13 Python
Django中的cookie和session
2019/08/27 Python
keras 两种训练模型方式详解fit和fit_generator(节省内存)
2020/07/03 Python
利用python下载scihub成文献为PDF操作
2020/07/09 Python
香港家用健身器材、运动器材及健康美容仪器专门店:FitBoxx
2019/12/05 全球购物
营业员演讲稿
2013/12/30 职场文书
文化活动实施方案
2014/03/28 职场文书
寄语学生的话
2014/04/10 职场文书
法制宣传月活动总结
2014/04/29 职场文书
校园游戏活动新闻稿
2014/10/15 职场文书
交通事故一次性赔偿协议书范本
2014/11/02 职场文书
欠款起诉书范文
2015/05/19 职场文书
使用Django框架创建项目
2022/06/10 Python
详解MySQL的内连接和外连接
2023/05/08 MySQL