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

本文实例为大家分享了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
, header
下载文件
php header 函数、php中的header函数、php header函数报错、php 钩子函数实例、php函数在线运行实例,以便于您获取更多的相关知识。

时间: 2024-09-20 06:40:31

php利用header函数下载各种文件_php实例的相关文章

php header函数下载文件实现代码

header函数最常用的不是用于下载而是用于发送http类的 跳转 它会执行最后一个,不过是有条件的,例如:  代码如下 复制代码 header('Location:http://www.111cn.net"); header('Location:http://www.g.cn'); header('Location:http://www.baidu.com'); 这个就会跳到百度 header('Location:http://www.111cn.net');echo '烈火网; header

PHP下利用header()函数设置浏览器缓存的代码_php技巧

这涉及到4种头标类型: Last-Modified(最后修改时间); Expires(有效期限); Pragma(编译指示): Cache-Control(缓存控制); 前三个头标属于HTTP1.0标准.头标Last-Modified使用UTC日期时间值.如果缓存系统发现Last-Modified值比页面缓存版本的更接 近当前时间,他就知道应该使用来自服务器的新版本. Expires 表明了缓存版本何时应该过期(格林威治标准时间).把它设置为一个以前的时间就会强制使用服务器上的页面. Pragm

详解PHP导入导出CSV文件_php实例

我们先准备mysql数据表,假设项目中有一张记录学生信息的表student,并有id,name,sex,age分别记录学生的姓名.性别.年龄等信息. 复制代码 代码如下: CREATE TABLE `student` (       `id` int(11) NOT NULL auto_increment,       `name` varchar(50) NOT NULL,       `sex` varchar(10) NOT NULL,       `age` smallint(3) NO

PHPMailer邮件类利用smtp.163.com发送邮件方法_php实例

第一步:需要下载PHPMailer文件包phpmailer-1.73.tar.gz 来自开源社区: http://phpmailer.sourceforge.net/ 第二步:确认你的服务器系统已经支持socket 如下图,通过phpinfo();查看是否支持sockets 如果没有这一项就请注意: socket 是属于PHP扩展部分,编译时必须给定一个用于./configure --enable-sockets 的配置选项. 第三步:把文件解压到你的web服务器目录下,调用类就可以了,说明:首

php header函数下载中文文件名乱码(ie/chrome)

在下载文件中加入下段代码即可解决  代码如下 复制代码 $name = rawurlencode($filename); header("Content-type: text/plain; charset=utf-8"); header("Content-Type: application/force-download"); header("Content-Type: application/octet-stream"); header(&quo

php多文件上传下载示例分享_php实例

复制代码 代码如下: <html><head>    <meta charset="utf-8">    <title>index_uploads</title></head><body>    <form action="uploads.php" method="post" enctype="multipart/form-data"&g

PHP header函数分析详解_php技巧

在php语言中,header()这个函数很有用的,尤其在用到ajax时候,他会帮你解决一些意想不到的问题.下面是header的一些详细讲解.希望对phper有帮助 复制代码 代码如下: <?php // fix 404 pages: header('HTTP/1.1 200 OK'); // set 404 header: header('HTTP/1.1 404 Not Found'); // set Moved Permanently header (good for redrictions

vb中利用xmlhttp来下载远程文件

xml|下载 建立一个vb工程,Project1添加引用:Microsoft scripting runtime,Microsoft Active Data Object,Microsoft MsXml Form1代码:    Public a As MSXML2.XMLHTTP   Private Sub Command1_Click()   Dim d As Class1   Set a = New MSXML2.XMLHTTP   a.open "get", "http

PHP stream_context_create()函数的使用示例_php实例

stream_context_create()函数是用来 创建打开文件的上下文件选项 ,用于fopen(),file_get_contents()等过程的超时设置.代理服务器.请求方式.头信息设置的特殊过程. 比如说,上篇php教程中gd库实现下载网页所有图片中,第10行: 利用了stream_context_create()设置代理服务器: 复制代码 代码如下: //设置代理服务器 $opts = array('http'=>array('request_fulluri'=>true));