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

相关文章

谈谈从phpinfo中能获取哪些值得注意的信息

谈谈从phpinfo中能获取哪些值得注意的信息

phpinfo函数 phpinfo函数 PHP中提供了PHPInfo()函数,该函数返回 PHP 的所有信息,包括了 PHP 的编译选项及扩充配置、PHP 版本、服务器信息及环境变量、P...

PECL方式安装php-mongodb扩展方法

PECL方式安装php-mongodb扩展方法

开始安装 全新虚拟机Ubuntu14.04,手动安装了apache2和php5;其余全没有。 那我们使用一条命令安装php扩展 sudo pecl install mongodb...

php数据访问之查询关键字

php数据访问之查询关键字

本文实例为大家分享了php查询操作的实现代码,供大家参考,具体内容如下 一、一个关键字查询 主页面: <html> <head> <meta http...

JS+PHP实现用户输入数字后显示最大的值及所在位置

JS+PHP实现用户输入数字后显示最大的值及所在位置

本文主要给大家介绍的是关于JS+PHP实现用户输入数字后显示最大的值及所在位置的相关内容,目的:分清JS PHP的区别,拓宽思维,下面来一起看看详细的介绍: 分析   ...

php处理单文件、多文件上传代码分享

php处理  单文件、多文件上传实例代码,供大家参考,具体内容如下  后台处理文件submit_form_process.php  <?...