入门客AI创业平台(我带你入门,你带我飞行)
博文笔记

一段php的文件下载函数

创建时间:2007-08-31 投稿人: 浏览次数:185

一段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("文件不存在!");
   }
}
?>
 
声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。