Go homepage(回首页)
Upload pictures (上传图片)
Write articles (发文字帖)

The author:(作者)归海一刀
published in(发表于) 2014/2/17 7:30:04
PHP实例教程:PHP实现文件安全下载的程序_[PHP教程]

PHP实例教程:PHP实现文件安全下载的程序_[PHP教程]

  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.dwww.cn/";


  $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直接输出文件了







If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)





QQ:154298438
QQ:417480759