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程序设计有所帮助。

相关文章

php面向对象之反射功能与用法分析

本文实例讲述了php面向对象之反射功能与用法。分享给大家供大家参考,具体如下: 个人对反射定义的理解: 首先得说说什么叫反射。对于一个新手来说,反射这个概念常常给人一种似懂非懂的 感觉,...

PHP函数积累总结

PHP函数积累总结

字符串 1、strtr(string,from,to)函数 把字符串中的字符from替换成to。 如果from和to长度不同,则格式化为最短的长度。   strtr(stri...

Thinkphp微信公众号支付接口

本文实例为大家分享了Thinkphp微信公众号支付接口,供大家参考,具体内容如下 第一步  先把文件夹的那两个图片 配置成一样的路径 除了域名要改 其他保持一致。 第二步&nb...

php随机生成数字字母组合的方法

本文实例讲述了php随机生成数字字母组合的方法。分享给大家供大家参考。具体如下: 直接上代码: 复制代码 代码如下:function getRandomString($len, $cha...

php检查页面是否被百度收录

最近需要检测网站内哪些页面没有被百度搜索引擎收录从而进行相关的调整。由于使用site命令一条条的去看实在是看不过来,就想到了使用php程序来批量处理一下,研究了一下,发现其实很简单,下面...