php隐藏实际地址的文件下载方法

yipeiwu_com5年前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目录与文件操作技巧。分享给大家供大家参考,具体如下: Demo1.php <?php //将一个路径赋给一个变量 //它目前来说,只是一个字...

PHP设计模式之装饰器模式定义与用法详解

本文实例讲述了PHP设计模式之装饰器模式定义与用法。分享给大家供大家参考,具体如下: 什么是装饰器模式 作为一种结构型模式, 装饰器(Decorator)模式就是对一个已有结构增加"装饰...

PHP实现的CURL非阻塞调用类

本文实例讲述了PHP实现的CURL非阻塞调用类。分享给大家供大家参考,具体如下: 前面一篇《PHP实现非阻塞模式的方法》文章讲述了PHP中实现非阻塞模式,其实如果只是HTTP的话,直接用...

php版微信数据统计接口用法示例

php版微信数据统计接口用法示例

本文实例讲述了php版微信数据统计接口用法。分享给大家供大家参考,具体如下: php版微信数据统计接口其实是非常的好用了在前版本还没有此功能是后面的版本增加上去了,下面来看一个php版微...

php利用gd库为图片添加水印

php利用gd库为图片添加水印

本文实例为大家分享了php利用gd库为图片添加水印的方法,供大家参考,具体内容如下 <?php $dst_path = '1.jpg';//目标图片 $src_pat...