服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

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

2019-06-20 15:32※WYF※ PHP教程

这篇文章主要为大家详细介绍了php利用header函数下载各种文件的代码,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了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教程Ajax+PHP实现的模拟进度条功能示例

    Ajax+PHP实现的模拟进度条功能示例

    这篇文章主要介绍了Ajax+PHP实现的模拟进度条功能,结合实例形式分析了php结合ajax动态读取文件实现进度条功能的相关操作技巧,需要的朋友可以参考下...

    chengqiuming4702019-06-18
  • PHP教程浅谈PHP无限极分类原理

    浅谈PHP无限极分类原理

    这篇文章主要介绍了PHP无限极分类原理,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...

    yangzailu4482019-05-31
  • PHP教程php面试中关于面向对象的相关问题

    php面试中关于面向对象的相关问题

    在本篇文章里小编整理了关于php面试中关于面向对象的相关问题内容,以及相关回答,有需要的朋友们学习下。...

    laozhang2592019-06-16
  • PHP教程浅谈PHPANALYSIS提取关键字

    浅谈PHPANALYSIS提取关键字

    这篇文章主要介绍了PHPANALYSIS如何提取关键字,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...

    瞪眼看太阳1302019-06-03
  • PHP教程php根据命令行参数生成配置文件详解

    php根据命令行参数生成配置文件详解

    这篇文章主要介绍了php根据命令行参数生成配置文件,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...

    ghostwu4982019-05-30
  • PHP教程PHP+jQuery实现即点即改功能示例

    PHP+jQuery实现即点即改功能示例

    这篇文章主要介绍了PHP+jQuery实现即点即改功能,涉及php数组读取、遍历及jQuery事件响应与动态修改页面元素属性相关操作技巧,需要的朋友可以参考下...

    ztblog1252019-06-11
  • PHP教程PHP中rename()函数的妙用讲解

    PHP中rename()函数的妙用讲解

    今天小编就为大家分享一篇关于PHP中rename()函数的妙用讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧...

    炫酷飞扬3972019-06-07
  • PHP教程PHP 数组和字符串互相转换实现方法

    PHP 数组和字符串互相转换实现方法

    PHP 中由于数组和字符串这两种变量类型是如此常用,以至于 PHP 具有两个函数,可以在字符串和数组之间互相进行转换...

    脚本之家1872019-05-28