php隐藏实际地址的文件下载方法

yipeiwu_com6年前PHP代码库

本文实例讲述了php隐藏实际地址的文件下载方法。分享给大家供大家参考。具体如下:

下面这段php代码可不透露实际的文件下载地址。

function download_document($filename,$path="",$mimetype="application/octet-stream")
{
 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
 header("Content-Disposition: attachment; filename = $filename");
 header("Content-Length: " . filesize($pathto . $filename));
 header("Content-Type: $mimetype");
 echo file_get_contents($pathto . $filename);
}

实现方法二:

<?php
$file = "1.txt";// 文件的真实地址(支持url,不过不建议用url)
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;
}
?>

希望本文所述对大家的php程序设计有所帮助。

相关文章

smarty表格换行实例

本文实例讲述了smarty表格换行的实现方法。分享给大家供大家参考。具体分析如下: 在foreach中加入 key=count 复制代码 代码如下:<table> ...

PHP安全性漫谈

PHP安全性漫谈

一、apache server安全性设置 1、以Nobody用户运行 一般情况下,Apache是由Root 来安装和运行的。如果Apache Server进程具有Root用户特权,那么它...

PHP如何根据文件头检测文件类型实例代码

前言 什么是文件头部Bom? 说白了,就是在保存文件的时候,文件前面会多出一串隐藏的字符,文件签名一般都在文件的头部,如果你用十六进制方式查看文件,你就可以看到文件的一些签名信息。如用u...

php递归创建和删除文件夹的代码小结

第一种方法: 复制代码 代码如下: <?php /** * 目录生成类 :UtilsMakeDir * @author yepeng * @since 2010.3.18 */ c...

php中使用redis队列操作实例代码

例1,入队操作:复制代码 代码如下:<?php$redis = new Redis();$redis->connect('127.0.0.1',6379);while(Tru...