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操作mongoDB实例分析

本文实例讲述了php操作mongoDB的方法。分享给大家供大家参考。具体分析如下: mongoDB数据库是一种以json格式存储的数据库,非常适用于各种应用开发,这里就来给各位朋友介绍一...

php页面缓存方法小结

本文实例总结了php页面缓存方法。分享给大家供大家参考。具体分析如下: 在php页面缓存主要用到的是ob系列函数,如ob_start(),ob_end_flush(),ob_get_co...

php数组函数序列之array_key_exists() - 查找数组键名是否存在

array_key_exists() 定义和用法 array_key_exists() 函数判断某个数组中是否存在指定的 key,如果该 key 存在,则返回 true,否则返回 fal...

php解析html类库simple_html_dom(详细介绍)

下载地址:https://github.com/samacs/simple_html_dom解析器不仅仅只是帮助我们验证html文档;更能解析不符合W3C标准的html文档。它使用了类似...

PHP中return 和 exit 、break和contiue 区别与用法

先说一下exit函数的用法。 作用: 输出一则消息并且终止当前脚本。 如果一段文本中包括多个以 结束的脚本,则exit退出当前所在脚本。 比如一篇php文本包括一下代码,则输出为worl...