【转】php 下载保存文件保存到本地的两种实现方法
来源:http://www.jb51.net/article/40485.htm
第一种:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<?php function downfile(){ $filename=realpath("resume.html"); //文件名 $date=date("Ymd-H:i:m"); Header( "Content-type: application/octet-stream "); Header( "Accept-Ranges: bytes "); Header( "Accept-Length: " .filesize($filename)); header( "Content-Disposition: attachment; filename= {$date}.doc"); echo file_get_contents($filename); readfile($filename); }downfile();?> |
或
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<?php function downfile($fileurl){ ob_start(); $filename=$fileurl; $date=date("Ymd-H:i:m"); header( "Content-type: application/octet-stream "); header( "Accept-Ranges: bytes "); header( "Content-Disposition: attachment; filename= {$date}.doc"); $size=readfile($filename); header( "Accept-Length: " .$size);} $url="url地址"; downfile($url);?> |
第二种:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<?php function downfile($fileurl){$filename=$fileurl;$file = fopen($filename, "rb"); Header( "Content-type: application/octet-stream "); Header( "Accept-Ranges: bytes "); Header( "Content-Disposition: attachment; filename= 4.doc"); $contents = "";while (!feof($file)) { $contents .= fread($file, 8192);}echo $contents;fclose($file); }$url="url地址";downfile($url);?> |
PHP实现下载文件的两种方法。分享下,有用到的朋友看看哦。
方法一:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<?php/*** 下载文件* header函数**/header('Content-Description: File Transfer');header('Content-Type: application/octet-stream');header('Content-Disposition: attachment; filename='.basename($filepath));header('Content-Transfer-Encoding: binary');header('Expires: 0′);header('Cache-Control: must-revalidate, post-check=0, pre-check=0′);header('Pragma: public');header('Content-Length: ' . filesize($filepath));readfile($file_path);?> |
了解php中header函数的用法。
方法二:
|
1
2
3
4
5
6
7
8
9
10
|
<?php//文件下载//readfile$fileinfo = pathinfo($filename);header('Content-type: application/x-'.$fileinfo['extension']);header('Content-Disposition: attachment; filename='.$fileinfo['basename']);header('Content-Length: '.filesize($filename));readfile($thefile);exit();?> |
第一种:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<?php function downfile(){ $filename=realpath("resume.html"); //文件名 $date=date("Ymd-H:i:m"); Header( "Content-type: application/octet-stream "); Header( "Accept-Ranges: bytes "); Header( "Accept-Length: " .filesize($filename)); header( "Content-Disposition: attachment; filename= {$date}.doc"); echo file_get_contents($filename); readfile($filename); }downfile();?> |
或
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<?php function downfile($fileurl){ ob_start(); $filename=$fileurl; $date=date("Ymd-H:i:m"); header( "Content-type: application/octet-stream "); header( "Accept-Ranges: bytes "); header( "Content-Disposition: attachment; filename= {$date}.doc"); $size=readfile($filename); header( "Accept-Length: " .$size);} $url="url地址"; downfile($url);?> |
第二种:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<?php function downfile($fileurl){$filename=$fileurl;$file = fopen($filename, "rb"); Header( "Content-type: application/octet-stream "); Header( "Accept-Ranges: bytes "); Header( "Content-Disposition: attachment; filename= 4.doc"); $contents = "";while (!feof($file)) { $contents .= fread($file, 8192);}echo $contents;fclose($file); }$url="url地址";downfile($url);?> |
PHP实现下载文件的两种方法。分享下,有用到的朋友看看哦。
方法一:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<?php/*** 下载文件* header函数**/header('Content-Description: File Transfer');header('Content-Type: application/octet-stream');header('Content-Disposition: attachment; filename='.basename($filepath));header('Content-Transfer-Encoding: binary');header('Expires: 0′);header('Cache-Control: must-revalidate, post-check=0, pre-check=0′);header('Pragma: public');header('Content-Length: ' . filesize($filepath));readfile($file_path);?> |
了解php中header函数的用法。
方法二:
|
1
2
3
4
5
6
7
8
9
10
|
<?php//文件下载//readfile$fileinfo = pathinfo($filename);header('Content-type: application/x-'.$fileinfo['extension']);header('Content-Disposition: attachment; filename='.$fileinfo['basename']);header('Content-Length: '.filesize($filename));readfile($thefile);exit();?> |
【转】php 下载保存文件保存到本地的两种实现方法的更多相关文章
- php 下载保存文件保存到本地的两种实现方法
这里的下载,指的是 弹出下载提示框. 第一种: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 <?php function downfile() { $filename ...
- javascript文件夹选择框的两种解决方案
javascript文件夹选择框的两种解决方案 解决方案1:调用windows 的shell,但会有安全问题. * browseFolder.js * 该文件定义了BrowseFolder()函数,它 ...
- web.config文件中配置数据库连接的两种方式
web.config文件中配置数据库连接的两种方式 标签: 数据库webconfig 2015-04-28 18:18 31590人阅读 评论(1)收藏举报 分类: 数据库(74) 在网站开发 ...
- 文件批量上传-统一附件管理器-在线预览文件(有互联网和没有两种)--SNF快速开发平台3.0
实际上在SNF里使用附件管理是非常简单的事情,一句代码就可以搞定.但我也要在这里记录一下统一附件管理器能满足的需求. 通用的附件管理,不要重复开发,调用尽量简洁. 批量文件上传,并对每个文件大小限制, ...
- Django学习——ajax发送其他请求、上传文件(ajax和form两种方式)、ajax上传json格式、 Django内置序列化(了解)、分页器的使用
1 ajax发送其他请求 1 写在form表单 submit和button会触发提交 <form action=""> </form> 注释 2 使用inp ...
- Java学习-014-文本文件写入实例源代码(两种写入方式)
此文源码主要为应用 Java 读取文本文件内容实例的源代码.若有不足之处,敬请大神指正,不胜感激! 第一种:文本文件写入,若文件存在则删除原文件,并重新创建文件.源代码如下所示: /** * @fun ...
- Java学习-013-文本文件读取实例源代码(两种数据返回格式)
此文源码主要为应用 Java 读取文本文件内容实例的源代码.若有不足之处,敬请大神指正,不胜感激! 1.读取的文本文件内容以一维数组[LinkedList<String>]的形式返回,源代 ...
- jQuery ajax调用后台aspx后台文件的两种常见方法(不是ashx)
在asp.net webForm开发中,用Jquery ajax调用aspx页面的方法常用的有两种:下面我来简单介绍一下. [WebMethod] public static string SayHe ...
- JAVA文件的两种读取方法和三种写入方法
在使用java对文件进行读写操作时,有多种方法可以使用,但不同的方法有不同的性能. 此文对常用的读写方法进行了整理,以备不时之需. 1.文件的读取 主要介绍两种常用的读取方法.按行读取和按字符块读取. ...
随机推荐
- Linux下串口编制【转】
串行口是计算机一种常用的接口,具有连接线少,通讯简单,得到广泛的使用.常用的串口是RS-232-C接口(又称EIA RS-232-C)它是在1970年由美国电子工业协会(EIA)联合贝尔系统.调制解调 ...
- BZOJ3506/1502 [CQOI2014]排序机械臂
传送门 依然是一道splay的区间操作,需要注意的是要把下标离散化后来表示splay的节点,我不知道怎么搞所以索性弄了个$ValuetoNode$,看样子没什么问题, 感觉他那个传下标的方法太暴力了. ...
- zabbix监控系列(2)之zabbix-agent安装
zabbix-agent的安装不用LAMP的支持,所以直接源码安装,make两下,改下配置文件即可,改完后可以把源码包直接压缩打包就可以给其他agent用了,可谓,一次编译,多台使用. NO1.解包压 ...
- openssl生成https证书 (转)
1.首先要生成服务器端的私钥(key文件):openssl genrsa -des3 -out server.key 1024运行时会提示输入密码,此密码用于加密key文件去除key文件口令的命令:o ...
- MySQL------MySQL与SQLServer数据类型的转换
MySQL SQLServer
- Codeforces 719B Anatoly and Cockroaches(元素的交叉排列问题)
题目链接:http://codeforces.com/problemset/problem/719/B 题目大意: 有一队蟑螂用字符串表示,有黑色 ‘b’ 和红色 'r' 两种颜色,你想使这队蟑螂颜色 ...
- 用Open Live Account写博文的第一篇文章,立个flag
在设置的时候出了点问题,还好有blog这种神器,直接上网址http://www.cnblogs.com/yishujun/p/5328617.html 高亮插件来自 http://www.cnblog ...
- HTTP错误404.13 - Not Found 请求筛选模块被配置为拒绝超过请求内容长度的请求
http://www.cnblogs.com/JKqingxinfeng/archive/2012/10/29/2744663.html HTTP错误404.13 - Not Found 请求筛选模块 ...
- Centos 7.0查看硬盘使用情况 命令
查看硬盘使用情况 df -hl [root@localhost ~]# df -hl文件系统 容量 已用 可用 已用%. 挂载点/dev/sda3 ...
- Flex入门笔记
Test_01.mxml <?xml version="1.0" encoding="utf-8"?> <viewer:BaseWidget ...