php强制文件下载(避免文件或图片直接在浏览器中打开)
来源:互联网 发布:怎么导入淘宝数据包 编辑:程序博客网 时间:2024/05/23 14:31
以下代码摘自FLEAPHP框架自带的文件下载助手类。
复制代码代码示例:<?php
/**
* FLEA_Helper_SendFile 类用于向浏览器发送文件
*
* 利用 FLEA_Helper_SendFile,应用程序可以将重要的文件保存在
* 浏览器无法访问的位置。然后通过程序将文件内容发送给浏览器。
* @site http://www.jbxue.com
*/
class FLEA_Helper_SendFile
{
/**
* 向浏览器发送文件内容
*
* @param string $serverPath 文件在服务器上的路径(绝对或者相对路径)
* @param string $filename 发送给浏览器的文件名(尽可能不要使用中文)
* @param string $mimeType 指示文件类型
*/
function sendFile($serverPath, $filename, $mimeType = 'application/octet-stream')
{
header("Content-Type: {$mimeType}");
$filename = '"' . htmlspecialchars($filename) . '"';
$filesize = filesize($serverPath);
$charset = FLEA::getAppInf('responseCharset');//根据实际文件编码类型,如utf-8,gbk
header("Content-Disposition: attachment; filename={$filename}; charset={$charset}");
header('Pragma: cache');
header('Cache-Control: public, must-revalidate, max-age=0');
header("Content-Length: {$filesize}");
readfile($serverPath);
exit;
}
}
以下代码摘自FLEAPHP框架自带的文件下载助手类。
复制代码代码示例:
<?php
/**
* FLEA_Helper_SendFile 类用于向浏览器发送文件
*
* 利用 FLEA_Helper_SendFile,应用程序可以将重要的文件保存在
* 浏览器无法访问的位置。然后通过程序将文件内容发送给浏览器。
* @site http://www.jbxue.com
*/
class FLEA_Helper_SendFile
{
/**
* 向浏览器发送文件内容
*
* @param string $serverPath 文件在服务器上的路径(绝对或者相对路径)
* @param string $filename 发送给浏览器的文件名(尽可能不要使用中文)
* @param string $mimeType 指示文件类型
*/
function sendFile($serverPath, $filename, $mimeType = 'application/octet-stream')
{
header("Content-Type: {$mimeType}");
$filename = '"' . htmlspecialchars($filename) . '"';
$filesize = filesize($serverPath);
$charset = FLEA::getAppInf('responseCharset');//根据实际文件编码类型,如utf-8,gbk
header("Content-Disposition: attachment; filename={$filename}; charset={$charset}");
header('Pragma: cache');
header('Cache-Control: public, must-revalidate, max-age=0');
header("Content-Length: {$filesize}");
readfile($serverPath);
exit;
}
}
/**
* FLEA_Helper_SendFile 类用于向浏览器发送文件
*
* 利用 FLEA_Helper_SendFile,应用程序可以将重要的文件保存在
* 浏览器无法访问的位置。然后通过程序将文件内容发送给浏览器。
* @site http://www.jbxue.com
*/
class FLEA_Helper_SendFile
{
/**
* 向浏览器发送文件内容
*
* @param string $serverPath 文件在服务器上的路径(绝对或者相对路径)
* @param string $filename 发送给浏览器的文件名(尽可能不要使用中文)
* @param string $mimeType 指示文件类型
*/
function sendFile($serverPath, $filename, $mimeType = 'application/octet-stream')
{
header("Content-Type: {$mimeType}");
$filename = '"' . htmlspecialchars($filename) . '"';
$filesize = filesize($serverPath);
$charset = FLEA::getAppInf('responseCharset');//根据实际文件编码类型,如utf-8,gbk
header("Content-Disposition: attachment; filename={$filename}; charset={$charset}");
header('Pragma: cache');
header('Cache-Control: public, must-revalidate, max-age=0');
header("Content-Length: {$filesize}");
readfile($serverPath);
exit;
}
}
0 0
- php强制文件下载(避免文件或图片直接在浏览器中打开)
- php强制文件下载(避免文件或图片直接在浏览器中打开)
- php中强制浏览器下载文件
- 强制文件下载而不是在浏览器里打开显示
- 在IE中打开或下载文件
- Java 下载文件不在浏览器中直接打开
- 一步一步分析解决PHP文件下载直接在网页中打开问题
- 控制浏览器端下载或打开文件
- 轻松下载.pdf文件(直接下载,而不是在IE浏览器里面打开)
- 轻松下载.pdf文件(直接下载,而不是在IE浏览器里面打开)
- 文件下载 浏览器直接打开文件而不是保存 相关问题
- 在浏览器中下载及打开文件的代码
- 直接打开下载文件
- php强制下载文件
- php 强制文件下载
- 强制文件下载php
- PHP强制下载文件
- PHP强制下载文件
- C/C++中void的作用,C/C++的语法对函数的一些影响
- windows PHP写的自动备份网站和MYSQL数据
- 80. Remove Duplicates from Sorted Array II
- CocoaPods管理第三方开源组件,入门介绍
- 实现 win2003 下 mysql 数据库每天自动备份
- php强制文件下载(避免文件或图片直接在浏览器中打开)
- 使用JS实现单击连接保存图片
- SUBLIME基本操作
- 将DedeCMS根目录下的data目录迁移到web以外目录方法
- display:inline-block之用法
- PHP next()移动数组内部指针到下一个元素的位置,并返回该元素 prev()
- 深拷贝与浅拷贝及页面跳转时赋值
- php数组指针如何移动到指定单元?
- laravel遇到"openssl extension is required..."