当前位置: 首页 > 图文教程 > 网络编程 > PHP > PHP实现文件安全下载的程序

PHP
抛开cookie使用session
实例讲解session的使用方法
zendoptimizer配置指南
开始了解 PHP V5 中的对象
apache 2.2.2 + PHP5.1.4 不能运行的解决办法.
Guard编码程序在指定虚拟主机上运行
PHP编程:探索字串的奥秘
用phpUnit帮你调试php程序
PHP开发中文件操作疑难FAQ
用PHP与XML 联手进行网站编程
php5手动最简安装方法
LAMP(Linux+Apache+MySQL+PHP)服务器的性能优化
PHP和COM
WINDOWS服务器安装多套PHP
让IIS支持PHP
PHP学习宝典-第一章
PHP学习宝典-第二章
PHP学习宝典-第二章 (续篇)
PHP学习宝典-第三章
PHP学习宝典-第三章 (续篇)

PHP实现文件安全下载的程序


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-14   浏览: 120 ::
收藏到网摘: n/a

PHP实现文件安全下载

程序如下:

以下为引用的内容:

  $file_name = "info_check.exe";

  $file_dir = "/public/www/download/";

  if (!file_exists($file_dir . $file_name)) { //检查文件是否存在

  echo "文件找不到";

  exit;

  } else {

  $file = fopen($file_dir . $file_name,"r"); //打开文件

  //输入文件标签

  Header("Content-type: application/octet-stream");

  Header("Accept-Ranges: bytes");

  Header("Accept-Length: ".filesize($file_dir . $file_name));

  Header("Content-Disposition: attachment; filename=" . $file_name);

  //输出文件内容

  echo fread($file,filesize($file_dir . $file_name));

  fclose($file);

  exit;}

  而如果文件路径是"http"或者"ftp"网址的话,则源代码会有少许改变,程序如下:

  $file_name = "info_check.exe";

  $file_dir = "www.www.ruanchen.com/";

  $file = @ fopen($file_dir . $file_name,"r");

  if (!$file) {

  echo "文件找不到";

  } else {

  Header("Content-type: application/octet-stream");

  Header("Content-Disposition: attachment; filename=" . $file_name);

  while (!feof ($file)) {

  echo fread($file,50000);

  }

  fclose ($file);

  }

这样就可以用PHP直接输出文件了