现在的位置: 首页 > 综合 > 正文

一段php的文件下载函数

2013年12月02日 ⁄ 综合 ⁄ 共 606字 ⁄ 字号 评论关闭

一段php的文件下载函数(可下任意文件类型)
使用方法
<?php
downFile("文件路径");
?>

   本文相关代码如下:
<?php
if(isset($_GET["file"])){
   downFile(realpath($_GET["file"]));
}
else
{
   echo("请输入文件路径!");
}
function downFile($sFilePath)
{
   if(file_exists($sFilePath)){
       $aFilePath=explode("/",str_replace("
//","/",$sFilePath),$sFilePath);
       $sFileName=$aFilePath[count($aFilePath)-1];
       $nFileSize=filesize ($sFilePath);
       header ("Content-Disposition: attachment; filename=" . $sFileName);
       header ("Content-Length: " . $nFileSize);
       header ("Content-type: application/octet-stream");
       readfile($sFilePath);
   }
   else
   {
       echo("文件不存在!");
   }
}
?>
 

抱歉!评论已关闭.