php下载文件的函数

PHP 2520 0 2012-08-28

php下载文件的函数
<?php
function download($file_dir,$file_name)
//参数说明:
//file_dir:文件所在目录
//file_name:文件名
{
  $file_dir = chop($file_dir);//去掉路径中多余的空格
  //得出要下载的文件的路径
  if($file_dir != '')
  {
    $file_path = $file_dir;
    if(substr($file_dir,strlen($file_dir)-1,strlen($file_dir)) != '/')
    $file_path .= '/';
    $file_path .= $file_name;
  } else {
    $file_path = $file_name;
  }
 
  //判断要下载的文件是否存在
  if(!file_exists($file_path))
  {
    echo '对不起,你要下载的文件不存在。';
    return false;
  }
 
  $file_size = filesize($file_path);
 
  header("Content-type: application/octet- tream");
  header("Accept-Range : byte ");
  header("Accept-Length: $file_size");
  header("Content-Disposition: attachment; filename=".$file_name);
 
  $fp= fopen($file_path,"r");
  $buffer_size = 1024;
  $cur_pos = 0;
 
  while(!feof($fp)&&$file_size-$cur_pos>$buffer_size)
  {
    $buffer = fread($fp,$buffer_size);
    echo $buffer;
    $cur_pos += $buffer_size;
  }
 
  $buffer = fread($fp,$file_size-$cur_pos);
  echo $buffer;
  fclose($fp);
  return true;
}
download("c:\AppServ\www\test.txt","txt");
?>

上一篇:PHP判断远程链接URL是否存在

下一篇:PHP面向对象分析设计的61条军规小结

讨论数量:3

天涯网魂 3 杠 5 星2013-09-30 12:21:41

header('Content-type: application/apk');
header('Content-Disposition: attachment; filename="hnw_for_android.apk"');
readfile('hnw_for_android.apk');

天涯网魂 3 杠 5 星2012-10-19 15:11:18

楼主补充:

//下载文件函数
function download($file_path,$down_name){
//参数说明:
//file_path:文件所在目录(包括文件名)
//down_name:下载时显示的文件名
if(empty($down_name)){ $down_name=$file_path; }
  //判断要下载的文件是否存在
  if(!file_exists($file_path))
  {
    echo '对不起,你要下载的文件不存在。'.$file_path;
    return false;
  }
 
  $file_size = filesize($file_path);
 
  header("Content-type: application/octet- tream");
  header("Accept-Range : byte ");
  header("Accept-Length: $file_size");
  header("Content-Disposition: attachment; filename=".$down_name);
 
  $fp= fopen($file_path,"r");
  $buffer_size = 1024;
  $cur_pos = 0;
 
  while(!feof($fp)&&$file_size-$cur_pos>$buffer_size)
  {
    $buffer = fread($fp,$buffer_size);
    echo $buffer;
    $cur_pos += $buffer_size;
  }
 
  $buffer = fread($fp,$file_size-$cur_pos);
  echo $buffer;
  fclose($fp);
  return true;
}
//==================================
//调用方法:
$FileName='./upfiles/data.txt';
download($FileName,'data.txt');

天涯网魂 3 杠 5 星2012-08-28 22:08:11

<?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("文件不存在!");
   }
}
?>

请先登录再发表讨论。 2024-04-29

天涯网魂
3 杠 5 星
TA 的文章
TA 的随言
TA 的资源链