来源: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 下载保存文件保存到本地的两种实现方法的更多相关文章

  1. php 下载保存文件保存到本地的两种实现方法

    这里的下载,指的是 弹出下载提示框. 第一种: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 <?php function downfile() {  $filename ...

  2. javascript文件夹选择框的两种解决方案

    javascript文件夹选择框的两种解决方案 解决方案1:调用windows 的shell,但会有安全问题. * browseFolder.js * 该文件定义了BrowseFolder()函数,它 ...

  3. web.config文件中配置数据库连接的两种方式

    web.config文件中配置数据库连接的两种方式 标签: 数据库webconfig 2015-04-28 18:18 31590人阅读 评论(1)收藏举报    分类: 数据库(74)  在网站开发 ...

  4. 文件批量上传-统一附件管理器-在线预览文件(有互联网和没有两种)--SNF快速开发平台3.0

    实际上在SNF里使用附件管理是非常简单的事情,一句代码就可以搞定.但我也要在这里记录一下统一附件管理器能满足的需求. 通用的附件管理,不要重复开发,调用尽量简洁. 批量文件上传,并对每个文件大小限制, ...

  5. Django学习——ajax发送其他请求、上传文件(ajax和form两种方式)、ajax上传json格式、 Django内置序列化(了解)、分页器的使用

    1 ajax发送其他请求 1 写在form表单 submit和button会触发提交 <form action=""> </form> 注释 2 使用inp ...

  6. Java学习-014-文本文件写入实例源代码(两种写入方式)

    此文源码主要为应用 Java 读取文本文件内容实例的源代码.若有不足之处,敬请大神指正,不胜感激! 第一种:文本文件写入,若文件存在则删除原文件,并重新创建文件.源代码如下所示: /** * @fun ...

  7. Java学习-013-文本文件读取实例源代码(两种数据返回格式)

    此文源码主要为应用 Java 读取文本文件内容实例的源代码.若有不足之处,敬请大神指正,不胜感激! 1.读取的文本文件内容以一维数组[LinkedList<String>]的形式返回,源代 ...

  8. jQuery ajax调用后台aspx后台文件的两种常见方法(不是ashx)

    在asp.net webForm开发中,用Jquery ajax调用aspx页面的方法常用的有两种:下面我来简单介绍一下. [WebMethod] public static string SayHe ...

  9. JAVA文件的两种读取方法和三种写入方法

    在使用java对文件进行读写操作时,有多种方法可以使用,但不同的方法有不同的性能. 此文对常用的读写方法进行了整理,以备不时之需. 1.文件的读取 主要介绍两种常用的读取方法.按行读取和按字符块读取. ...

随机推荐

  1. CDN网络(一)之典型的CND架构与HTTP协议的缓存控制

    前言 本人以前在CDN厂商蓝汛就职过一年时间,利用脑子里还残留的一些CDN知识,结合现有的书籍材料,写点东西. what's the CDN CDN(content delivery Network) ...

  2. 深入理解JavaScript中创建对象模式的演变(原型)

    深入理解JavaScript中创建对象模式的演变(原型) 创建对象的模式多种多样,但是各种模式又有怎样的利弊呢?有没有一种最为完美的模式呢?下面我将就以下几个方面来分析创建对象的几种模式: Objec ...

  3. setInterval js

    $('#start_scan').on('click',function(){ if(timer == undefined){ timer = setInterval(scan,1000) start ...

  4. ArcGIS Server开发教程系列(8)ArcGIS API for Javascript-控件(小部件)(续)纯代码

    完整代码如下: <html> <head> <meta http-equiv="Content-Type" content="text/ht ...

  5. JavaScript学习笔记——基本知识

    JavaScript学习的教程来自后盾网 1>JavaScript的放置和注释 1.输出工具 A.alert(); B.document.write(); C.prompt("&quo ...

  6. Android学习笔记——Handler(二)

    对比请看http://blog.sina.com.cn/s/blog_78c913e30100uqmf.html 以下代码是MainActivity.java中的代码 package com.exam ...

  7. [ngRepeat:dupes] Duplicates in a repeater are not allowed. Use 'track by' expression to specify uniq

    angularjs 使用ng-repeat报错 <div ng-init="words = ['高校','高校','高校']" ng-repeat="word in ...

  8. Mono

    mono (Novell公司开发的跨平台·NET运行环境) http://baike.baidu.com/link?url=iGl5T4-pmpJwB01mQxHq95bJcrCvR_JMNNfP0u ...

  9. 安装CentOS7重启后提示License information

    Initial setup of CentOS Linux (core) ) [x] Creat user ) [!] License information (no user will be cre ...

  10. PHP 函数整理 (用过的)

    1:$_SERVER['DOCUMENT_ROOT'] $_SERVER['DOCUMENT_ROOT']是PHP预定义的几个变量之一.作用是:获取当前运行脚本所在的文档根目录.该根目录是由服务器配置 ...