您的位置主页 > 编程专区 > Php > 一段php的文件下载函数(可下任意文件类型)

一段php的文件下载函数(可下任意文件类型)

2009-09-25    文章来源:互联网    浏览次数:1151     分享文章
 
  1. <?php
  2. if(isset($_GET["file"])){
  3.    downFile(realpath($_GET["file"]));
  4. }
  5. else
  6. {
  7.    echo("请输入文件路径!");
  8. }
  9. function downFile($sFilePath)
  10. {
  11.    if(file_exists($sFilePath)){
  12.        $aFilePath=explode("/",str_replace("\\","/",$sFilePath),$sFilePath);
  13.        $sFileName=$aFilePath[count($aFilePath)-1];
  14.        $nFileSize=filesize ($sFilePath);
  15.        header ("Content-Disposition: attachment; filename=" . $sFileName);
  16.        header ("Content-Length: " . $nFileSize);
  17.        header ("Content-type: application/octet-stream");
  18.        readfile($sFilePath);
  19.    }
  20.    else
  21.    {
  22.        echo("文件不存在!");
  23.    }
  24. }
  25. ?>

 

文章评论(查看全部)

昵 称 *
电子邮箱 *
网 址      3 + 9 = ?