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. pip3命令报错Fatal error in launcher: Unable to create process using '"d:\old_files\py3.6\python.exe" "E:\py3.6\Scripts\pip3.exe" list'

    cmd输入pip3 list命令报错 Fatal error in launcher: Unable to create process using '"d:\old_files\py3.6 ...

  2. Boostrap常用组件英文名

    dropdownlisttabsearchVertical TabSidebar with tabssidebarExpandable Panel ListFiltered Attendees Lis ...

  3. 每天一个Linux命令(64)shutdown命令

        shutdown以一种安全的方式关闭系统.     (1)用法:     用法:  shutdown [参数] [时间]     (2)功能:     功能:  系统关机命令,shutdown ...

  4. Linux用户和用户组管理 用户配置和管理的相关文件

    用户信息文件 /etc/passwd 这个文件中保存的就是系统中所有的用户及其对应的用户主要信息. 文件格式 :  第1字段 第2字段 第3字段 第4字段 第5字段 第6字段 第7字段 用户名称 密码 ...

  5. mysql利于cte进行分组统计并计算占比

    CTE定义:一个公共表表达式(common table expression)是一个命名的临时结果集,它在一条单独的语句中有效,可以在语句中被引用多次. CTE基本语法: WITH cte1 [(co ...

  6. window7 3G/4G拨号操作

    Win7系统Modem拨号操作指导:https://wenku.baidu.com/view/bb855b1dc77da26925c5b0e1.html 拨号上网设置APN,拨号号码,帐号和密码:ht ...

  7. 【Flask】Sqlalchemy limit, offset slice操作

    ### limit.offset和切片操作:1. limit:可以限制每次查询的时候只查询几条数据.2. offset:可以限制查找数据的时候过滤掉前面多少条.3. 切片:可以对Query对象使用切片 ...

  8. Linux基础三---打包压缩&vim&系统的初始化和服务

    一,常用命令——tar&vim 1. tar [参数]  文件名  [路径] 参数: -c :建立一个压缩文件的参数指令(create 的意思):     -x :解开一个压缩文件的参数指令! ...

  9. MongoDB命令语法小用

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using MongoDB; ...

  10. FullPage.js全屏滚动插件

    一.介绍 fullPage.js是一个基于jQuery的插件,他能够很方便.很轻松的制作出全屏网站,主要功能有: 1.支持鼠标滚动 2.多个回调函数 3.支持手机.平板触摸事件 4.支持CSS3动画 ...