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


Posted in PHP onAugust 12, 2013

第一种:

<?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();
?>


<?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);
?>

第二种:

<?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实现下载文件的两种方法。分享下,有用到的朋友看看哦。

方法一:

<?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函数的用法。

方法二:

<?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 相关文章推荐
ob_start(),ob_start('ob_gzhandler')使用
Dec 25 PHP
一个自定义位数的php多用户计数器代码
Mar 11 PHP
PHP实现多服务器session共享之NFS共享的方法
Mar 16 PHP
php自动加载的两种实现方法
Jun 21 PHP
php URL跳转代码 减少外链
Jun 25 PHP
浅析application/x-www-form-urlencoded和multipart/form-data的区别
Jun 22 PHP
PHP自毁程序(慎用)
Jul 09 PHP
php数字运算验证码的实现代码
Jul 30 PHP
PHP读MYSQL中文乱码的快速解决方法
Oct 01 PHP
php表单习惯用的正则表达式
Oct 11 PHP
PHP+Apache环境中如何隐藏Apache版本
Nov 24 PHP
php让json_encode不自动转义斜杠“/”的方法
Apr 27 PHP
CodeIgniter上传图片成功的全部过程分享
Aug 12 #PHP
php实现MD5加密16位(不要默认的32位)
Aug 12 #PHP
php中的Base62类(适用于数值转字符串)
Aug 12 #PHP
PHP实现把数字ID转字母ID
Aug 12 #PHP
PHP计算2点经纬度之间的距离代码
Aug 12 #PHP
php读取csv文件后,uft8 bom导致在页面上显示出现问题的解决方法
Aug 10 #PHP
浅析php中常量,变量的作用域和生存周期
Aug 10 #PHP
You might like
批量获取memcache值并按key的顺序返回的实现代码
2011/06/14 PHP
Php无限级栏目分类读取的实现代码
2014/02/19 PHP
dedecms中使用php语句指南
2014/11/13 PHP
laravel实现简单用户权限的示例代码
2019/05/28 PHP
php面向对象重点知识分享
2019/09/27 PHP
PHP终止脚本运行三种实现方法详解
2020/09/01 PHP
json-lib出现There is a cycle in the hierarchy解决办法
2010/02/24 Javascript
JavaScript中使用构造函数实现继承的代码
2010/08/12 Javascript
JS遍历Json字符串中键值对先转成JSON对象再遍历
2014/08/15 Javascript
AngularJS 自定义指令详解及示例代码
2016/08/17 Javascript
javascript删除html标签函数cIsHTML
2017/01/09 Javascript
Angular 4依赖注入学习教程之简介(一)
2017/06/04 Javascript
Angular 2父子组件数据传递之@Input和@Output详解(下)
2017/07/05 Javascript
基于vue2实现上拉加载功能
2017/11/28 Javascript
Vue路由模块化配置的完整步骤
2019/08/14 Javascript
js正则匹配多个全部数据问题
2019/12/20 Javascript
整理 node-sass 安装失败的原因及解决办法(小结)
2020/02/19 Javascript
Vue实现点击导航栏当前标签后变色功能
2020/08/19 Javascript
[04:12]第二届DOTA2亚洲邀请赛选手传记-Newbee.Sccc
2017/04/03 DOTA
简介Python中用于处理字符串的center()方法
2015/05/18 Python
将Python代码打包为jar软件的简单方法
2015/08/04 Python
Python中datetime模块参考手册
2017/01/13 Python
python字符串格式化方式解析
2019/10/19 Python
Python运行提示缺少模块问题解决方案
2020/04/02 Python
Django 解决新建表删除后无法重新创建等问题
2020/05/21 Python
python如何输出反斜杠
2020/06/18 Python
浅谈Python 参数与变量
2020/06/20 Python
python中用ctypes模拟点击的实例讲解
2020/11/26 Python
什么是SQL Server的确定性函数和不确定性函数
2016/08/04 面试题
新闻专业个人自我评价
2013/09/21 职场文书
多媒体教室标语
2014/06/26 职场文书
白酒营销策划方案
2014/08/17 职场文书
2015年银行客户经理工作总结
2015/04/01 职场文书
在js中修改html body的样式
2021/11/11 Javascript
继承Win10缺点!教你关闭Win11烦人的网络搜索
2021/11/23 数码科技
Python数组变形的几种实现方法
2022/05/30 Python