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 函数 下载各种文件的更多相关文章

  1. 微信小程序开发 -- 通过云函数下载任意文件

    微信小程序开发 -- 通过云函数下载任意文件 1.云开发介绍 ​ 微信小程序开发者众所周知,小程序开发拥有许多限制,当我还是一个菜鸟入门的时候,第一关就卡在了没有备案域名的HTTP请求上面,那时候云开 ...

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

    在php中header函数的使用很大,header不但可以向客户端发送原始的 HTTP 报头信息,同时还可以直接实现文件下载操作 header函数最常用的不是用于下载而是用于发送http类的 跳转 它 ...

  3. PHP——使用header()函数下载文件

    思路:先指明内容的MIME类型,内容描述,内容长度(也即文件大小). 一.下载txt文件的程序: <?phpheader('Content-Type:text/plain');header('C ...

  4. C# 利用FTP自动下载xml文件后利用 FileSystemWatcher 监控目录下文件变化并自动更新数据库

    using FtpLib; using System; using System.Collections.Generic; using System.ComponentModel; using Sys ...

  5. 利用base64函数,对文件进行转码加密

    设计此种编码是为了使二进制数据可以通过非纯 8-bit 的传输层传输,例如电子邮件的内容就是通过base64转码后传输的.Base64-encoded后, 数据要比原始数据多占用 33% 左右的空间. ...

  6. php自定义函数: 下载远程文件 httpcopy

    <?php function httpcopy($url, $file="", $timeout=60) { $file = empty($file) ? pathinfo( ...

  7. PHP自定义函数: 下载远程文件

    function httpcopy($url, $file="", $timeout=60) { $file = empty($file) ? pathinfo($url,PATH ...

  8. python 下载大文件

    当使用requests的get下载大文件/数据时,建议使用使用stream模式. 当把get函数的stream参数设置成False时,它会立即开始下载文件并放到内存中,如果文件过大,有可能导致内存不足 ...

  9. 利用php CI force_download($filename, $data) 下载.csv 文件解决文件名乱码,文件内容乱码

    利用php CI force_download($filename, $data) 下载.csv 文件解决文件名乱码,文件内容乱码 2014-07-31 12:53 1047人阅读 评论(0) 收藏  ...

随机推荐

  1. Binary Search in Java

    关于折半查找中的几个注意点. Version 1: public static <T extends Comparable<? super T>> int binSearch( ...

  2. C#加快Bitmap的访问速度

    在对Bitmap图片操作的时候,有时需要用到获取或设置像素颜色方法:GetPixel 和 SetPixel, 如果直接对这两个方法进行操作的话速度很慢,这里我们可以通过把数据提取出来操作,然后操作完在 ...

  3. SQL SERVER 2005 Express版, 精简版 下载

      Microsoft SQL Server 2005 Express Edition(数据库) https://www.microsoft.com/zh-CN/download/details.as ...

  4. MYSQL:基础—主键

    MYSQL:基础—主键 1.什么是主键 表中的每一行都应该具有可以唯一标识自己的一列(或一组列).而这个承担标识作用的列称为主键. 如果没有主键,数据的管理将会十分混乱.比如会存在多条一模一样的记录, ...

  5. 17南宁区域赛 J - Rearrangement 【规律】

    题目链接 https://nanti.jisuanke.com/t/19976 题意 给出 一个n 然后 给出 2*n 个数 可以重新排列成两行 然后 相邻的两个数 加起来 不能被三整除 可以上下相邻 ...

  6. C++中引用编译过的C代码为什么要用“extern c”

    函数经过编译系统的翻译成汇编,函数名对应着汇编标号.  因为C编译函数名与得到的汇编代号基本一样,如:fun()=>_fun, main=>_main  但是C++中函数名与得到的汇编代号 ...

  7. RHEL(或CentOS)中关于逻辑卷( Logical Volume Manager,LVM)的一些概念及使用LVM的例子

    1.逻辑卷(logical volumes,LV) 卷管理在物理存储之上的抽象层,它使你能够创建逻辑存储卷.和直接使用物理存储相比,这从很多方面提供了更大的灵活性.比如,使用逻辑卷,你将不再受物理磁盘 ...

  8. IOS中大文件拷贝算法

    + (void)copyFileFromPath:(NSString *)fromPath toPath:(NSString *)toPath { //每次读取数据大小 #define READ_SI ...

  9. centos6 多段Ip添加脚本

    #!/bin/bash export device=`ifconfig|grep eth0|head -n 1|awk '{print ($1)}'`export ipcfg_pre="/e ...

  10. 中文乱码问题(页面乱码,eclipse乱码,请求响应乱码)

    1.首先在开发工具eclipse中设置工作空间和文件编码格式,详情参见   http://www.cnblogs.com/lixiang1993/p/7345161.html 2.在eclipse的安 ...