【转】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.文件的读取 主要介绍两种常用的读取方法.按行读取和按字符块读取. ...
随机推荐
- 9月27日Bootstrap
Bootstrap,来自 Twitter,是目前最受欢迎的前端框架.Bootstrap 是基于 HTML.CSS.JAVASCRIPT 的,它简洁灵活,使得 Web 开发更加快捷.Bootstrap ...
- Android学习笔记——ProgressBar
该工程的功能是实现进度条的显示,按以下按钮进度条增加10% 以下代码是MainActivity.java中的代码 package com.example.progressbar; import and ...
- SVN中Branch和Merge实践
参考资料:http://blog.csdn.net/eggcalm/article/details/6606520 branch主要用于新功能的开发,开发过程中不断从trunk merge revis ...
- 入门:HTML表单与Java 后台交互(复选框提交)
仅仅给出部分关键代码: HTML form code: <form action="JavaFormTest" method="post" name=&q ...
- Sublime Text 3065
Package Control安装 安装方法:https://packagecontrol.io/installation 用Package Control安装插件 1.按下Ctrl+Shift+P调 ...
- Jsp与servlet的区别
Jsp与servlet的区别 2011-12-09 16:27:47 分类: Java 1.jsp经编译后就变成了Servlet.(JSP的本质就是Servlet,JVM只能识别java的类,不能识 ...
- C#实现Excel模板导出和从Excel导入数据
午休时间写了一个Demo关于Excel导入导出的简单练习 1.窗体 2.引用office命名空间 添加引用-程序集-扩展-Microsoft.Office.Interop.Excel 3.封装的Exc ...
- QQ空间HD(3)-Modal的切换效果总结
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { UIViewController ...
- webapi支持跨域访问
写在前面 在实际应用中,跨域请求还是比较常见的,如何上接口直接支持跨域的访问呢? demo 场景项目A有个接口用来获取用户列表,现在项目b也有个功能需要加载用户列表.这两个项目在两个域名下,至少端口好 ...
- addEventListener和on的区别
为什么需要addEventListener? 先来看一个片段: html代码 <div id="box">追梦子</div> 用on的代码 1 window ...