-
php下实现文件下载实现代码
文章介绍了利用php来实现读取文件并且下载的代码,php要下载文件必须用到header函数,大家可参考一下,代码如下:
- <?php
- $file = 'monkey.gif';
- if (file_exists($file)) {
- header('Content-Description: File Transfer');
- header('Content-Type: application/octet-stream');
- header('Content-Disposition: attachment; filename='.basename($file));
- 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($file));
- ob_clean();
- flush();
- readfile($file);
- exit;
- }
- ?>
以上代码是下载代码,接下来贴一段在线预览pdf文件的代码:
- <?php
- public function fddAction()
- {
- // get attachment location
- $attachment_location = $_SERVER["DOCUMENT_ROOT"] . "/pdf/fdd/sample.pdf";
- if (file_exists($attachment_location)) {
- // attachment exists
- // send open pdf dialog to user
- header('Cache-Control: public'); // needed for i.e.
- header('Content-Type: application/pdf');
- header('Content-Disposition: inline; filename="sample.pdf"');
- readfile($attachment_location);
- die(); // stop execution of further script because we are only outputting the pdf
- } else {
- die('Error: File not found.');
- }
- }
- ?>
原文链接:http://www.phpfensi.com/php/20140103/1041.html
栏目列表
最新更新
01 Web应用模式
python 约瑟夫生者小游戏 用list实现
DBPack 赋能 python 微服务协调分布式事务
PYTHON之SELENIUM调用XPATH实现网页操作
Python双人五子棋
matplotlib可视化系列之【排版】
C# 编写一个简单易用的 Windows 截屏增强工
Python 引用其他路径下的module
【Python - 基础】基础是一切升华的根本
自动化办公:手机号码提取器,使用正则
三大常用数据库事务详解之三:事务运行
三大常用关系型数据库事务详解之二:基
三大关系型数据库事务详解之一:基本概
MongoDB常用命令(2)
MongoDB基本介绍与安装(1)
SQLServer触发器调用JavaWeb接口
SQL Server索引的原理深入解析
SqlServer2016模糊匹配的三种方式及效率问题
SQL中Truncate的用法
sqlserver 多表关联时在where语句中慎用tri
Vue(1)Vue安装与使用
JavaScript 语言入门
js将一段字符串的首字母转成大写
纯原生html编写的h5视频播放器
H5仿原生app短信验证码vue2.0组件附源码地
TypeScript(4)接口
TypeScript(3)基础类型
TypeScript(2)WebStorm自动编译TypeScript配置
TypeScript(1)介绍与安装
ES6 - promise(1)