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图片加水印原理(超简单的实例代码)

文字水印: 复制代码 代码如下: $w = 80; $h = 20; $im = imagecreatetruecolor($w,$h); $textcolor = imagecolor...

php微信开发之自定义菜单实现

php微信开发之自定义菜单实现

编辑模式和开发模式是有冲突的。所以我们启用微信公众号的开发模式之后,那些菜单是看不到的哦。不过现在个人订阅号是不可以使用高级开发者模式的,如自定义菜单,不过我们还是可以通过测试号来测试一...

phpQuery占用内存过多的处理方法

phpQuery是一个用php实现的类似jQuery的开源项目,可以在服务器端以jQuery的语法形式解析网页元素。 相对于正则或其它方式匹配网页方式,phpQuery使用起来要方便的多...

php自定义urlencode,urldecode函数实例

本文实例讲述了php自定义urlencode,urldecode函数。分享给大家供大家参考。具体如下: //配合JavaScript的ajaxObject函数, 对字串进行转码. f...

php获得客户端浏览器名称及版本的方法(基于ECShop函数)

php获得客户端浏览器名称及版本的方法(基于ECShop函数)

本文实例讲述了php获得客户端浏览器名称及版本的方法。分享给大家供大家参考,具体如下: 看到ecshop中有这么一个函数get_user_browser(),获取浏览器的名称和版本。虽然...