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. Python Variable Scope

    Python中的变量的作用域有时会让像我这样的初学者很头疼. 其实只需要掌握以下两点: 1. Python能够改变变量作用域的代码段是def.class.lamda;    而if/elif/else ...

  2. 构建Ruby开发环境(Windows+Eclipse+Aptana Plugin)

    1.安装Ruby ①.从http://rubyinstaller.org/downloads/下载安装包:rubyinstaller-2.2.5-x64.exe,直接安装.(so easy) 2.安装 ...

  3. (转)【腾讯 TMQ】 接口测试用例设计

    导语 这是我在其他的开源社区看到的一篇分享帖子.这篇文章的目的只是为大家提供一个思路,但是实现成本太高了,因为一个接口设计的接口测试用例很多,一般公司的接口数量几百到上千不等,每一个接口都设计这么多测 ...

  4. OpenGL学习进程(6)第四课:点、边和图形(一)点

    本节是OpenGL学习的第四个课时,下面介绍OpenGL点的相关知识:     (1)点的概念:     数学上的点,只有位置,没有大小.但在计算机中,无论计算精度如何提高,始终不能表示一个无穷小的点 ...

  5. 每天一个Linux命令(61)killall命令

        killall命令用进程的名字来杀死进程.     (1)用法:     用法:  killall [ -egiqvw ] [ -signal ] [进程名称] 格式:killall -< ...

  6. 【leetcode刷题笔记】Minimum Window Substring

    Given a string S and a string T, find the minimum window in S which will contain all the characters ...

  7. Python编程-函数进阶二

    一.生成器补充 1.什么是生成器? 可以理解为一种数据类型,这种数据类型自动实现了迭代器协议(其他的数据类型需要调用自己内置的__iter__方法),所以生成器就是可迭代对象. 2.生成器分类 (1) ...

  8. Linux下解压分包文件zip(zip/z01/z02)

    分包压缩的zip文件不能被7z解压,且这种格式是Windows才能创建出来,在Linux下不会以这种方式去压包.下面是在Linux下处理这种文件的做法: 方法一: cat xx.z01 xx.zip ...

  9. 使用shell自动备份数据库

    全备份 #!/bin/sh #mysql地址 #检测用户是否手动输入了密码 mysql_host="" #mysql用户 mysql_user="" #mysq ...

  10. 基于docker环境,搭建 jetty环境, 部署java项目

    前提: 1.Ubuntu 系统. 2.docker环境已经安装好. 实现步骤: 1.上docker hub 下载jetty docker 镜像. 执行命令:$ sudo docker pull jet ...