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中switch语句用法详解

本文介绍php中的switch语句的用法,它跟其他语句中的switch用法差不多的,但注意有有一个break语句。 PHP中switch语句的标准语法: switch (expre...

PHP 类商品秒杀计时实现代码

要求要有小时分钟秒的实时倒计时的显示,用户端修改日期时间不会影响到倒计时的正常显示(也就是以服务器时间为准)。 其实这和很多的考试等系统的时间限制功能同样的要求。 总不能用ajax每秒都...

PHP标准类(stdclass)用法示例

本文实例讲述了PHP标准类(stdclass)用法。分享给大家供大家参考,具体如下: php是内置标准类的(stdclass) <?php $obj = new std...

PHP实现给定一列字符,生成指定长度的所有可能组合示例

PHP实现给定一列字符,生成指定长度的所有可能组合示例

本文实例讲述了PHP实现给定一列字符,生成指定长度的所有可能组合。分享给大家供大家参考,具体如下: 给定一列字符,生成指定长度的所有可能的组合: 如:a,b,c,d,e 或 0-9&nb...

PHP实现定时执行任务的方法

本文实例讲述了PHP实现定时执行任务的方法,代码简单实用。分享给大家供大家参考。 具体实现方法如下: ignore_user_abort(true); //客户端断开时忽略脚本中止(...