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 相关文章推荐
一棵php的类树(支持无限分类)
Oct 09 PHP
支持数组的ADDSLASHES的php函数
Feb 16 PHP
判断PHP数组是否为空的代码
Sep 08 PHP
php中如何防止表单的重复提交
Aug 02 PHP
关于js和php对url编码的处理方法
Mar 04 PHP
PHP中file_get_contents高?用法实例
Sep 24 PHP
如何让CI框架支持service层
Oct 29 PHP
PHP CURL或file_get_contents获取网页标题的代码及两者效率的稳定性问题
Nov 30 PHP
PHP实现图片不变型裁剪及图片按比例裁剪的方法
Jan 14 PHP
[原创]PHP正则删除html代码中a标签并保留标签内容的方法
May 23 PHP
PHP设计模式之观察者模式定义与用法示例
Aug 04 PHP
php xhprof使用实例详解
Apr 15 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
对盗链说再见...
2006/10/09 PHP
抓取YAHOO股票报价的类
2009/05/15 PHP
php简单socket服务器客户端代码实例
2015/05/18 PHP
PHP 实现从数据库导出到.csv文件方法
2017/07/06 PHP
php使用redis的几种常见操作方式和用法示例
2020/02/20 PHP
初识Javascript小结
2015/07/16 Javascript
根据user-agent判断蜘蛛代码黑帽跳转代码(js版与php版本)
2015/09/14 Javascript
js仿网易表单及时验证功能
2017/03/07 Javascript
前端主流框架vue学习笔记第二篇
2017/07/26 Javascript
angular中实现li或者某个元素点击变色的两种方法
2017/07/27 Javascript
web前端页面生成exe可执行文件的方法
2018/02/08 Javascript
微信小程序开发之自定义tabBar的实现
2018/09/06 Javascript
electron + vue项目实现打印小票功能及实现代码
2018/11/25 Javascript
Vue2.0使用嵌套路由实现页面内容切换/公用一级菜单控制页面内容切换(推荐)
2019/05/08 Javascript
前后端常见的几种鉴权方式(小结)
2019/08/04 Javascript
JS PHP字符串截取函数实现原理解析
2020/08/29 Javascript
[34:08]2018DOTA2亚洲邀请赛3月29日 小组赛B组 VP VS EG
2018/03/30 DOTA
python实现简单的计时器功能函数
2015/03/14 Python
介绍Python的Urllib库的一些高级用法
2015/04/30 Python
解决uWSGI的编码问题详解
2017/03/24 Python
spyder常用快捷键(分享)
2017/07/19 Python
Python 由字符串函数名得到对应的函数(实例讲解)
2017/08/10 Python
tensorflow没有output结点,存储成pb文件的例子
2020/01/04 Python
Django使用list对单个或者多个字段求values值实例
2020/03/31 Python
Python如何获取文件路径/目录
2020/09/22 Python
selenium+headless chrome爬虫的实现示例
2021/01/08 Python
美国高级音响品牌:Master&Dynamic
2018/07/05 全球购物
Ibatis如何调用存储过程
2015/05/15 面试题
大学生自荐书范文
2013/12/10 职场文书
我的五年职业生涯规划
2014/01/23 职场文书
2014年社区重阳节活动策划方案
2014/09/16 职场文书
毕业生入职感言
2015/07/31 职场文书
校运会广播稿
2015/08/19 职场文书
初二数学教学反思
2016/02/17 职场文书
基于Python编写简易版的天天跑酷游戏的示例代码
2022/03/23 Python
python神经网络ResNet50模型
2022/05/06 Python