思路:先指明内容的MIME类型,内容描述,内容长度(也即文件大小)。

一、下载txt文件的程序:

<?php
header('Content-Type:text/plain');
header('Content-Disposition:attachment;filename=y.txt');
header('Content-Length:'.filesize('y.txt'));
readfile('y.txt');
?>

二、下载图片文件程序:

<?php
$filename='images/01.gif';
header('Content-type:image/gif');
header('content-disposition:attachment;filename='.$filename);
header('content-length:'.filesize($filename));
readfile($filename);
?>

三、下载html文件的程序:

<?php
$file='r.html';
header('Content-Type:text/html');
header('Content-Disposition:attachment;filename='.$file);
header('Content-Length:'.filesize($file));
readfile($file);
?>

PHP——使用header()函数下载文件的更多相关文章

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

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

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

    http://www.php.net/manual/en/function.readfile.php <?php /** * 下载文件 * header函数 * */ dl_file($_GET ...

  3. header头 下载文件 参数详解

    header( header( header( header( header( header( header( header( header( //2 浏览器不会响应缓存 //1 Public指示响应 ...

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

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

  5. php 下载文件的函数

    通过函数完成下载文件的PHP功能代码 function download($url, $filename) { // 获得文件大小, 防止超过2G的文件, 用sprintf来读 $filesize = ...

  6. 记录一个php强制下载文件函数

    整理个经常用的强制下载文件的函数,之前就说把一些常用的东西整理出来结果老是没时间,最近陆续把这些东西整理下. public function download() { $id = $this-> ...

  7. 【php】header下载文件后,文件变大的问题;(ob_clean()清空缓存)

    事由: 按照常理,为了下载一个生产的文件,都是使用header函数,指定下文件信息,然后开始下载,但是下载后发现,文件体积总是比源文件要大2个字节,源文件是24字节,下载后是26字节,查看服务器返回的 ...

  8. php自定义函数: 下载本地服务器的大文件

    // 使用方法 $file_path = './a.zip'; // 只能是本地服务器文件, 多大的文件都支持!! down_file($file_path); // 函数参数: 服务器文件路径,下载 ...

  9. php header下载文件 无法查看原因

    php header下载文件 无法查看原因 php header下载文件 下方函数可以下载单个文件 function download($file_url){ if(!isset($file_url) ...

随机推荐

  1. hdu 3579 Hello Kiki (中国剩余定理)

    Hello Kiki Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  2. [转]Oracle 经验集

    -- 转换字符串为日期格式 to_date('2011-12-30 11:54:30','yyyy-MM-dd:hh24:mi:ss') Oracel 用 C# 连接,Microsoft 自带的 Sy ...

  3. HTML DOM 方法

    一.HMTL DOM对象 --方法和属性 1.1常用的方法. 1.getElementByld( id )方法 --获取带有指定id 的节点( 元素 ) 2.appendChild( node )方法 ...

  4. AppScan 测试需要输入用户名密码的网站

    Cisco有专门的网页版的AppScan,使用前需要向有关的team申请account和权限. account和权限申请成功后,登录AppScan网站,创建自己的文件夹目录,然后在自己的目录下新建sc ...

  5. Object.create()方法的低版本兼容问题

    Object.prototype.create=(function(){ if(Object.prototype.create){return Object.prototype.create}else ...

  6. agsXMPP参考代码

    agsXMPP 1.删除好友 XmppCon.RosterManager.RemoveRosterItem(node.RosterItem.Jid); 2.注销用户 void userConn_OnL ...

  7. FreeSWITCH 1.2.5.3 Step by Step Install

    Ubuntu: apt-get -y install build-essential automake autoconf git-core wget libtool apt-get -y instal ...

  8. ZOJ 3705 Applications 模拟

    #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include< ...

  9. ViewPager循环显示

    好久没有写博客了,今天加一个ViewPager页面的循环显示,添加了一个删除页面的小按钮: MainActivity.java package com.yt.viewpagerlooper; impo ...

  10. js如何找到方法在哪个js文件

    在Console窗口中输入var f = methodA.prototype.constructor;console.log(f); 网络搜索到的方法.