PHP-强制文件下载 (Force download)

简介: 在APACHE经过SSL加密后,原来下载文件(如XLS)的代码,在原来好用的情况下,出现“Internet Explorer 无法下载 **.php (来自**网站)。Internet Explorer无法打开该 internet 网站。
在APACHE经过SSL加密后,原来下载文件(如XLS)的代码,在原来好用的情况下,出现“Internet Explorer 无法下载 **.php (来自**网站)。Internet Explorer无法打开该 internet 网站。请求的网站不可用,或找不到,请以后再试。 ”提示,而且下载的不是我们指定的文件,好像是在下载PHP源文件了。
 
使用下面的方法经验证后可用:
 
<?php
/**
* Force a file to be download instead of displayed.
*/

function force_download  ($filePath$name$mimetype = '')  {
       // File's mimetype is not set?
      
if (empty($mimetype))  {
              
$mimetype = 'application/octet-stream';
       }


      
// Start sending headers
      
header("Pragma: public"); // required
      
header("Expires: 0");
      
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
      
header("Cache-Control: private", false);   // required for certain browsers
      
header("Content-Transfer-Encoding: binary");
      
header("Content-Type: " . $mimetype);
      
header("Content-Length: " . filesize($filePath));
      
header("Content-Disposition: attachment; filename='" . $name . "';" );

      
//read data
      
readfile(
$filePath );
       exit();
}
?>
 
 
http://www.jb51.net/article/15553.htm这里提到的方法似乎行不通。(我测试没通过)。
目录
相关文章
|
机器学习/深度学习 关系型数据库 MySQL
PHP文件下载函数(从MySQL提取文件路径)
<?php header("Content-type: text/html;charset=utf-8"); require("include/MysqlConn.php"); // 获得ID $id=$_GET['id']; // 判断是否有参数 if (!$id) {     die ("缺少.
2722 0
|
应用服务中间件 PHP Apache